Seb
Seb

Reputation: 3564

Cancel animations in UITableView while scrolling

I am a bit confused and don't have much else to turn to. I have an iOS application that gets messages from a server and populates it into a cell in a UITableView. Every time a certain object gets updated from the server that update triggers a cell animation. In our case we have a table of scores in a game. If someone loses a point their cell would flash red and if they gain a point their cell would flash green. Simple concept that works well and our users like the feedback. What they don't like is how when they scroll they see the cells have random colors associated with it and stay there for about a second. I believe that it has something to do with the way that the cells are being recycled. I tried various checks regarding this issue, checking that the cell has the same value, setting the color manually to white, etc. and non of these methods work.

All in all I am trying to only show the animation when the cell has been in view and not when the users is scrolling through the UITableView. Anyone have any advice or ideas?

Thanks

Upvotes: 1

Views: 2494

Answers (2)

David Rönnqvist
David Rönnqvist

Reputation: 56625

What is happening

Since table views reuse their cells you are ending up reused cells that is being flashed even though they shouldn't.

How to solve it

You can restore the reused cell to its default state either in preprareForReuse: on the custom table view cell or in the tableView:willDisplayCell:forRowAtIndexPath: delegate method (as ACB mentioned).

Since you are animating with UIView animations, you only need to change the value of the animated property to cancel the animation and return to the default state.

If you would have user Core Animation for the flash

If the animation was done using Core Animation you would instead have called removeAllAnimations on the animating layer. (For a flashing animation you probably wouldn't change the property on the layer, instead you would tell the animation to autoreverse. This would mean that removing the animation would change the color back to its original value.)

Upvotes: 3

iDev
iDev

Reputation: 23278

One option is to remove the animation and reset the color in willDisplayCell delegate method to the default color. If that doesn't work you can set the dequeueReusableCellWithIdentifier as follows,

NSString *aCellIdentifier = [NSString stringWithFormat:@"%d",indexPath.row];
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:aCellIdentifier];     

But this will be a problem if you have a lot data to display as it will create memory issues. Note that in this case you will not be reusing the table cells.

Upvotes: 0

Related Questions