Search code examples
iosuitableviewnstimer

Update UITableView every second


I am wondering what is the proper way of showing countdown in fairly complex UITableViewCells, in each cell one countdown with different time.

I could have NSTimer for each cell separately but then the cells wouldnt update at same time. I could have one NSTimer for all cells but calling reloadData on uitableview is fairly expensive to do every second, isnt it?

Obviously the needs to be updated only in visible cells.

Any ideas, best practice, know how on this matter?


Solution

  • Some thoughts on how I would implement this:

    • Create a custom class for the cell's prototype and implement an update method in that class. This method updates the label.
    • Create the timer in the custom TableViewController class
    • Whenever the timer fires, iterate through the cells via the indexPath. UITableView.CellForRowAtIndexPath returns nil for cells which aren't visible.
    • Call the cell's update method for each of these visible cells.