Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTimer for UITableviewcell similar to World Clock app

I would like to build a few simple countdown timers in a UITableview. I noticed the World Clock app has animated clocks in each of it's rows. What is the best method to firing an NSTimer to update each table row? Can a single timer be run that updates all cells or should I have a custom view subclass with built in timer that is added to each row?

I am just trying to get a proper course of action.

like image 889
SonnyBurnette Avatar asked Sep 27 '10 16:09

SonnyBurnette


2 Answers

One timer is sufficient. When it fires, ask the table view for the visible cells and update them one by one. You could also just call reloadData when the timer fires but depending on how you draw your cells you might do more work than necessary and you cannot use animations.

like image 126
Ole Begemann Avatar answered Oct 19 '22 22:10

Ole Begemann


Just know that you need to schedule the timer to fire more than once a second. NSTimer isn't a precision device--you're scheduling it for the next available processor slot after the time duration you specify. So a 1 second NSTimer WON'T fire in exactly a second, and can actually accumulate quite a lot of error over time. You want it to come alive several times a second, look at the current time, notice when a second has passed, and do the UI/data updates appropriate to that fact.

like image 36
Dan Ray Avatar answered Oct 19 '22 22:10

Dan Ray