Alex Nichol
Alex Nichol

Reputation: 7510

reloadTable after animated row deletion

I have a table view with cells that can be deleted. When a cell is deleted, I would like to display a deletion animation such as UITableViewRowAnimationFade. The problem is that my table view's cells have alternating background colors, so I need to -reloadData after the animation completes to fix the colors of all cells below the deleted cell.

So, incase you haven't read this far, I need a way to run some code after a table view animation is complete. I am currently using performSelector:withObject:afterDelay: and hardcoding an approximation of the animation delay. This method works, but there must be a better way to do it:

NSIndexPath * path = [listTable indexPathForCell:deletingCell];
NSArray * indexPaths = [NSArray arrayWithObject:path];
[listTable deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[listTable performSelector:@selector(reloadData) withObject:nil afterDelay:0.35];

In this case I am assuming that UITableViewRowAnimationFade will take less than 0.35 seconds to complete.

Upvotes: 0

Views: 552

Answers (1)

smdvlpr
smdvlpr

Reputation: 1088

You could also use NSTimer with the same delay (although unneeded, -performSelector:withObject:afterDelay: works fine).

Another option would be to loop through the cells after deletingCell (path) and redraw them, or add the index paths to an array and call reloadRowsAtIndexPaths:withRowAnimation: as shown below. Then you could also use the UITableViewRowAnimationFade

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

Upvotes: 2

Related Questions