Programmatically move/animate a UITableView row from one position to another
I have a UITableView and I want to programmatically move one row from position N1 to position N2 and I would like it to animate from the old location to the new. I开发者_如何学Python've looked through the UITableView documentation and I'm only seeing inserts, reloads, and deletes. Do you know of a way I can do this programmatically?
A couple of notes:
I know that I can animate the deletion from location N1 and animate the insertion to location N2 at the same time. That's my fallback, but I'd like the user to understand that it is truly moving from N1 to N2.
I'm not talking about allowing the user to drag it from one place to another. I understand how to do that, I'm looking for a way to initiate and animate the move programmatically.
I've dealt with something kind of like this in the past and the solution is not pretty but it does work.
Basically you have to do some math to calculate where on the screen the row is. Then adjust that based on the y
property of the contentOffset
of your UITableView
.
You can do that like this:
[myView convertPoint:localPosition toView:nil];
Then you would construct a new UIView
that's identical to your view. Set its frame to be directly on top of the UITableViewCell
you're trying to move. Add it as a subview of your app's main UIWindow
.
You'll want to set userInteractionEnabled
or scrollEnabled
to NO
on your UITableView
while all of this is going on so that the user can't muck with the position of things throughout the process (just remember to set it back to YES
when everything is done animating).
Then animate it to the new position however makes the most sense.
I understand you may have to scroll the UITableView
in the process of all of this, which complicates things considerably, but you can animate that scrolling as well.
You'll of course have to do similar operations on the row you're animating to if it is necessary in your app.
Like I said its not easy or pretty. But I do understand what you're trying to accomplish and I think the effect is super awesome when it's pulled off properly.
If anyone has a better idea of how to pull this off without going insane I'd love to know, but this type of implementation is the best I've been able to do.
[table beginUpdates];
[table moveRowAtIndexPath:indexPath toIndexPath:destIndexPath];
[table endUpdates];
If you're using iOS 5 or hight you could use the method:
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
For example:
//move last cell one row up.
NSIndexPath *oldPath = [NSIndexPath indexPathForRow:[items count] - 1 inSection:0];
NSIndexPath *newPath = [NSIndexPath indexPathForRow:[items count] - 2 inSection:0];
[[self tableView] moveRowAtIndexPath:oldPath toIndexPath:newPath];
Check out apple's documentation here.
I guess your best fallback would indeed be the deletion and insertion method, something like:
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:oldPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newNewPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
精彩评论