我有一个定制的UITableViewCell
它使用了 NSTimer
.屏幕弹出时如何停止或使计时器无效。
我试过了
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
但它似乎不起作用。
提前致谢。
最佳答案
您可以使用 NSNotificationCenter 停止 UITableviewCell 的计时器
UITableViewCell 文件中的第 1 步
override func awakeFromNib() {
super.awakeFromNib()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.stopTimer), name: "stopTimer", object: nil)
}
func stopTimer(){
if self.couponTimer != nil {
self.couponTimer?.invalidate()
self.couponTimer = nil
}
}
ViewController 文件中的第 2 步
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
NSNotificationCenter.defaultCenter().postNotificationName("stopTimer", object: nil)
}
关于ios - 与自定义 UITableViewCells 关联的 NSTimers 离开屏幕时无效/停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23556685/