使用当前运行循环来解决问题会给你带来各种麻烦。解决此问题的最简单方法是在单元上具有NSTimer
属性,并在单元格为willDisplay
/willEndDisplay
时启动/停止该属性。
class CustomCell: UITableViewCell {
var timer: NSTimer?
func startTimer() -> Void {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateUI:", userInfo: nil, repeats: true)
}
func stopTimer() -> Void {
timer?.invalidate()
timer = nil
}
func updateUI(sender: NSTimer?) -> Void {
// update your label here
}
}
class ViewController: UIViewController, UITableViewDelegate {
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if let cell = cell as? CustomCell {
cell.startTimer()
}
}
func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if let cell = cell as? CustomCell {
cell.stopTimer()
}
}
}
我不会_dare_以这种方式使用NSRunLoop。你只是在问问题。 – gnasher729
我对此声明投了票。 – NCFUSN