monsabre
monsabre

Reputation: 2099

move an UITableViewCell with animation between 2 sections in a UITableView

In one app, there are 2 sections in an UITableView. I hope to move an UITableViewCell with animation from one section to another section in the UITableView, is it possible?

Welcome any comment

Thanks

Upvotes: 1

Views: 3152

Answers (1)

MarkPowell
MarkPowell

Reputation: 16540

UITableView has the answer:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

So, delete the entry from the first row and add it to the second. There are a number of animations you can select from.

As an example (sorry doing this from memory without a compiler). Assumptions: section0Data holds data for your first section, section1Data holds it for your second.

[self.section0Data removeObjectAtIndex:removeIdx];
[self.section1Data insertObject:entry atIndex:insertIdx];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:removeIdx inSection:0]]
                                              withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:insertIdx inSection:1]]
                                              withRowAnimation:UITableViewRowAnimationFade];

Upvotes: 7

Related Questions