I have ones UITableViewcellsthat are created at different points in time, and I would like each of them to have an independent timer that starts when an object is added usingreloadData()
This is what I have done so far.
import UIKit
var timer = Timer()
var blinkStatus:Bool! = false
var time = 300
class LiveViewCell: UITableViewCell {
let str = String(format:"%02d:%02d", (time / 60), (time % 100))
func processTimer() {
if time > 0 {
time -= 1
timeRemainingLbl.text = String(time)
} else if time == 0 {
timer.invalidate()
timeRemainingLbl.text = "Due!"
timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(LiveViewCell.blinkTimer), userInfo: nil, repeats: true)
}
}
func blinkTimer() {
if blinkStatus == false {
timeRemainingLbl.textColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:1.0)
blinkStatus = true
} else if blinkStatus == true {
timeRemainingLbl.textColor = UIColor.clear
blinkStatus = false
}
}
@IBOutlet weak var tableNumberLeftLabel: UILabel!
@IBOutlet weak var guestNumbersLabel: UILabel!
@IBOutlet weak var timeInTableLabel: UILabel!
@IBOutlet weak var tableNumberLbl: UILabel!
@IBOutlet weak var timeRemainingLbl: UILabel!
var table: Table!
func configureLiveCell(_ NT: Table) {
layer.cornerRadius = 20
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(LiveViewCell.processTimer), userInfo: nil, repeats: true)
tableNumberLbl.text = "T" + String(NT.number)
timeRemainingLbl.text = String(time)
}
}
The problem occurs when it configureLiveCellis called whenever I create a new cell. It seems that the timer is speeding up, and I would like each timer to be independent.
source
share