0

I am using following method to delete table cell but it doesn't invalidate timer which is running inside that cell.

 [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationBottom];
aBilal17
  • 2,974
  • 2
  • 17
  • 23
Vishwanath Deshmukh
  • 619
  • 1
  • 10
  • 32
  • That is because tablecell are cached and reused, better not to but a `NSTimer` inside the cell. – rckoenes Apr 18 '18 at 14:58
  • I am changing image inside table cell every 1 second based and reading imagename from variable present inside table view controller. imagename gets changed every 1 second in tableview controller so I have to change image in tablecell for every 1 second. How should I do it? – Vishwanath Deshmukh Apr 18 '18 at 15:06
  • 1
    Use `reloadRowsAtIndexPaths` and pass the Index Path of the cell that needs updating from the `UIViewController` – rckoenes Apr 18 '18 at 15:08
  • I want to change only imageview. e.g. If reload cell every 0.2 seconds doesn't it hamper the performance ? – Vishwanath Deshmukh Apr 18 '18 at 15:21
  • Maybe, depends on what else is in the call. But since Cell are reused and cached you should not keep something like a timer in a cell. You might use KVO to check it the object bound to the cell has changed. – rckoenes Apr 18 '18 at 15:23
  • Do you have any example for KVO table cells. I little struggled with KVO but couldn't remove observer. When I scroll my table cell and again visit the same cell it re-adds observer and tablecells got repeated.. – Vishwanath Deshmukh Apr 18 '18 at 15:32
  • Implemented with KVO and working. Thank You! – Vishwanath Deshmukh Apr 18 '18 at 19:14

1 Answers1

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.