Take the 2-minute tour ×

My goal is to have a UITableViewCell slide off one side of the screen (like in Twitter) and then slide back on from the other side. I'm able to make the cell slide off the screen to the right, but I can't seem to figure out how to get it to slide back onto the screen from the left right after. Here's my code to slide it off to the right:

UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
[cell.layer setAnchorPoint:CGPointMake(0, 0.5)];
[cell.layer setPosition:CGPointMake(cell.frame.size.width, cell.layer.position.y)];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position.x"];
[animation setRemovedOnCompletion:NO];
[animation setDelegate:self];
[animation setDuration:0.14];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[cell.layer addAnimation:animation forKey:@"reveal"];

Thanks for any help!

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

UITableView provides methods to insert and remove rows with animation and handles all the animation for you. Try something like this:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:myIndexPaths
[tableView endUpdates];

And then to slide in a new cell:

[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:myNewIndexPaths
[tableView endUpdates];

The beginUpdates/endUpdates methods cause the animations to be batched up and executed all at once.

Beware of 2 things:

  1. With a lot of table data, the insert can take a long time (expecially if you are essentially replacing the whole table). Calling [tableView reloadData] works better in this case, but the deleteRowsAtIndexPath:withRowAnimation: can still be used for a nice visual effect.

  2. You have to be sure that the actual data backing your table changes accordingly and correctly. That is to say, if you are removing or inserting rows form the table, then your array (or whatever) that is feeding the table must also correctly reflect how many rows are in the table at each step.

share|improve this answer
This won't work in my case (see my comments on XJones' answer). What I really need is a way to do the reverse of what I have in my question. –  edc1591 Apr 12 '11 at 20:56
I used the techniques I described to animate in and out custom table cells, for what it's worth. Good luck! –  MarkGranoff Apr 12 '11 at 21:02
add comment

The behavior you want should be done by animating subviews of the cell, not the cell itself. The cell should stay in place in the table. Create a custom cell with the subviews you want in the contentArea and then slide those on/off as you like.

The standard UIView block based animations should work great for this as well.

share|improve this answer
I actually have to animate the actual cell and not the view because this is for a MobileSubstrate tweak for jailbroken devices. I'm doing this on cells in the MobileMusicPlayer app and they use a custom UITableViewCell which implements a custom UIView as the content view and I don't have access to those views. So, creating a custom cell is also not an option. –  edc1591 Apr 12 '11 at 20:21
I'll take your word for it. Generally, if you have access to the cell then you have access to the contentView property (doesn't matter what the superclass of the cell is) and that can be animated. Animating the actual cell may end up having unpredictable side effects/behavior. Hope you get it working. –  XJones Apr 12 '11 at 20:24
Calling contentView on the cells returns nil. And obviously, the cells in MobileMusicPlayer are not empty. And Apple did not put a property in their custom cells to access the content view. –  edc1591 Apr 12 '11 at 20:25
uncle! when you get outside of the "normal" way of doing things you're outside of my expertise. –  XJones Apr 12 '11 at 20:27
add comment

Your Answer


