【发布时间】:2018-08-21 02:39:51
【问题描述】:
我想点击表格单元格并启动一个计时器。每个单元格都应该有自己的计时器。我设法设置了计时器,但不是独立设置的,当我设置一个时,所有单元格都设置好了。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let currentCell = tableView.cellForRow(at: indexPath) as! StaffTableViewCell
if isRunning == false {
currentCell.cellImageView.image = UIImage(data: UIImagePNGRepresentation(#imageLiteral(resourceName: "greendot"))!)
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
counter += 1
let hourPortion = String(format: "%02d", counter / 3600)
let minutesPortion = String(format: "%02d", counter / 60)
let secondsPortion = String(format: "%02d", counter % 60)
//print("\(hourPortion):\(minutesPortion):\(secondsPortion)")
timeTotal = "\(hourPortion):\(minutesPortion):\(secondsPortion)"
currentCell.timerLabel.text = timeTotal
isRunning = true
}
}
tableView.reloadData()
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
timer.invalidate()
isRunning = false
let currentCell = tableView.cellForRow(at: indexPath) as! StaffTableViewCell
if let data = UIImagePNGRepresentation(#imageLiteral(resourceName: "reddot")) {
currentCell.cellImageView.image = UIImage(data: data)
}
tableView.reloadData()
}
【问题讨论】:
-
只有一个计时器,每次触发时,更新有“计时器运行”的单元格中的经过时间。
-
我已经尝试过 Paulw11 但我是初学者并且做得不好,所以我需要编码方面的帮助。谢谢
-
@Paulw11 在计时器运行时,您会使用什么方法来更新各个单元格中的值?
-
我正在使用领域数据库,但这是我拥有的所有代码,谢谢
标签: ios swift uitableview cocoa-touch