iOS中UITableView的每个单元格上的时间倒数 [英] Time Countdown on each cell of UITableView in iOS
问题描述
我想在tableview的每个单元格上显示倒数计时器.
I want to show countdown timer on every cell of tableview.
步骤
我有来自服务器的报价清单,其中包含到期时间.因此,在此之后,我想与UITableView上的每个单元格一起显示此到期倒数计时器.
I have offer list from server with expire time. So after then I want show this expire countdown timer with each of cell on UITableView.
我正在做这样的事情.但是没有结果.
I am doing something like this. But no result.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TimeCell
cell.expiryTimeInterval = 2
return cell
}
**我正在启动计时器的我的细胞类".打印步骤1和仅Step2 **
**My Cell Class where I am starting timer. Printing Step1 & Step2 only **
class TimeCell: UITableViewCell {
@IBOutlet weak var myLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
private var timer: Timer?
private var timeCounter: Double = 0
var expiryTimeInterval : TimeInterval? {
didSet {
startTimer()
print("Step 1 \(expiryTimeInterval!)")
}
}
private func startTimer() {
if let interval = expiryTimeInterval {
timeCounter = interval
print("Step 2 \(interval)")
if #available(iOS 10.0, *) {
timer = Timer(timeInterval: 1.0,
repeats: true,
block: { [weak self] _ in
guard let strongSelf = self else {
return
}
strongSelf.onComplete()
})
} else {
timer = Timer(timeInterval: 1,
target: self,
selector: #selector(onComplete),
userInfo: nil,
repeats: true)
}
}
}
@objc func onComplete() {
print("Step 3")
guard timeCounter >= 0 else {
timer?.invalidate()
timer = nil
return
}
myLabel.text = String(format: "%d", timeCounter)
timeCounter -= 1
}
}
推荐答案
我建议您创建一个自定义单元格,然后在该单元格类中,根据服务器中的值创建一个计时器.这样,每个单元都有自己的计时器.那可能就是你想要的.在当前的实现中,您的计时器处理程序不知道单元格或其行号.
I’d rather suggest you to create a custom cell and in that cell class, create a timer based on your value from server. This way every cell would have its own timer. That’s what you probably want. In your current implementation, your timer handler is unaware of cell or its row number.
class MyCustomCell: UITableViewCell {
@IBOutlet weak private var myLabel: UILabel!
private var timer: Timer?
private var timeCounter: Double = 0
var expiryTimeInterval: TimeInterval? {
didSet {
startTimer()
}
}
private func startTimer() {
if let interval = expiryTimeInterval {
timeCounter = interval
if #available(iOS 10.0, *) {
timer = Timer(timeInterval: 1.0,
repeats: true,
block: { [weak self] _ in
guard let strongSelf = self else {
return
}
strongSelf.onComplete()
})
} else {
timer = Timer(timeInterval: 1.0,
target: self,
selector: #selector(onComplete),
userInfo: nil,
repeats: true)
}
}
}
@objc func onComplete() {
guard timeCounter >= 0 else {
timer?.invalidate()
timer = nil
return
}
myLabel.text = String(format: "%d", timeCounter)
timeCounter -= 1
}
}
用法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MyCustomCell
cell.expiryTimeInterval = 10
return cell
}
转到您的单元格 nib
或 storyboard
,然后将 TimeCell
的类名称更改为 MyCustomCell
Go to you cell nib
or storyboard
and change the class name for TimeCell
to MyCustomCell
这篇关于iOS中UITableView的每个单元格上的时间倒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!