ios - 与自定义 UITableViewCells 关联的 NSTimers 离开屏幕时无效/停止

标签 ios objective-c uitableview nstimer

我有一个定制的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/

相关文章:

IOS UITableview 在 IOS6 中滚动不流畅

objective-c - 在 Xcode 4.2 中使用 Objective-C ARC 时,如何防止对象被释放?

ios - 使静态单元格快速执行操作

ios - 如何快速隐藏customView中的按钮

ios - 从iOS在后台将位置坐标发送到服务器

objective-c - 使用未声明的标识符

ios - 在选择单元格之前,不会显示 URL 中的 Swift 表格 View 图像

ios - Audiokit 4.5.2 - 无法通过终端中的 cocoa pod 安装

iphone - 如果部署目标 < 基本 sdk,如何检查是否使用了不可用的方法?

ios - 单击后 UITableViewCell 调整大小,约束发生变化