如何使tablecell中的NSTimer无效

时间:2018-04-18 14:53:56

标签: objective-c nstimer

我使用以下方法删除表格单元格,但它不会使在该单元格内运行的计时器无效。

 [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationBottom];

1 个答案:

答案 0 :(得分:0)

Here's what I would do instead:

First, I would ensure each cell has a unique identifier of some sort. Then, I would create an NSMutableArray or NSMutableDictionary as a property of your UIViewController that stores each cell's corresponding NSTimer. Next, if a reference to the NSTimer from a table view cell is absolutely necessary, I can create a weak reference to the NSTimer in the dictionary. Then, when I go to delete the table view cell, I can also invalidate the timer by looking it up in the NSArray/NSDictionary and then deleting it.