Dale Dale - 6 months ago 24
Objective-C Question

Blinking image in table cell

I want to have a blinking image (swaps between two images) in table cells.

What seemed most natural to me is as follows:


  • Set a NSTimer to goes every .5 seconds, calling -blink:

  • In -blink: I toggle a blinkStatus variable on/off

  • In -cellForRowAtIndexPath:, I set the image depending upon the blinkStatus



Now, I also call [myTableView reloadData] in the -blink: method, so thinks get updated.

This seems to make sense; the state of the cells is actually changing (the image is being changed), so having the table update makes sense.

This works. However, the reloadData seems to interfere with the user interaction:

In table editing mode if the user selects (-) to delete, and the Delete button appears, it will disappear when reloadData occurs. It's easy enough only do the reloadData if [myTableView.editing] is false. (And it's not a big problem to not have blinking during editing mode.)

However, the reloadData still seems to mess things up (e.g. clear cell selection). I also worry about the efficiency of calling reloadData every .5 seconds.

Is there a smoother way to get that image blinking? I was thinking of saving a pointer to any blinking cells, and update the .image property in the timer. But this seems like a bad idea, since cells are reused for different rows of the table (ouch), and I doubt that simply updating the .image property would update the table without a reloadData.

Answer

Did you try to use [UIImageView animationImages] property? Just provide your images, set required duration and animationRepeatCount and call startAnimating