UITableView moveSection
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection
Parameters of [UITableView moveSection]
section
The index of the section to move.
newSection
The index in the table view that is the destination of the move for the section. The existing section at that location slides up or down to an adjoining index position to make room for it.
Discussion of [UITableView moveSection]
You can combine section-move operations with section-insertion and section-deletion operations within a beginUpdates–endUpdates block to have all changes occur together as a single animation.
Unlike the section-insertion section row-deletion methods, this method does not take an animation parameter. For sections that are moved, the moved section animates straight from the starting position to the ending position. Also unlike the other methods, this method allows only one section to be moved per call. If you want multiple section moved, you can call this method repeatedly within a beginUpdates–endUpdates block.
UITableView moveSection example.
So, for example, to animate the changes:
0 C -> A 0
1 F H 1
2 H C 2
3 K K 3
you could use:
[tableView deleteSections:@[1] withRowAnimation:UITableViewRowAnimationAutomatic]; // delete F
[tableView insertSections:@[0] withRowAnimation:UITableViewRowAnimationAutomatic]; // insert A
[tableView moveSection:2 toSection:1]; // move H
[tableView moveSection:0 toSection:2]; // move C (implied by other changes so not necessary)
0 C -> A 0
1 F H 1
2 H C 2
3 K K 3
you could use:
[tableView deleteSections:@[1] withRowAnimation:UITableViewRowAnimationAutomatic]; // delete F
[tableView insertSections:@[0] withRowAnimation:UITableViewRowAnimationAutomatic]; // insert A
[tableView moveSection:2 toSection:1]; // move H
[tableView moveSection:0 toSection:2]; // move C (implied by other changes so not necessary)
Example of [UITableView moveSection].
If some set of moves are implied by the other changes that are made, it is not necessary to explicitly make them. For example to rotate section A to the bottom of the table:
0 A -> B 0
1 B C 1
2 C D 2
3 D A 3
the following are logically equivalent:
[tableView moveSection:0 toSection:3];
and
[tableView moveSection:1 toSection:0];
[tableView moveSection:2 toSection:1];
[tableView moveSection:3 toSection:2];
However they are animated slightly differently. In the first case, section A is animated moving down over the others, while in the second case the three sections moving up are animated over section A.
Finally, it appears that when animating a set of changes that involves some sections that move down and others that move up, the sections that don't move are on the bottom, the sections that move down are above them, and the sections that move up are on top.
0 A -> B 0
1 B C 1
2 C D 2
3 D A 3
the following are logically equivalent:
[tableView moveSection:0 toSection:3];
and
[tableView moveSection:1 toSection:0];
[tableView moveSection:2 toSection:1];
[tableView moveSection:3 toSection:2];
However they are animated slightly differently. In the first case, section A is animated moving down over the others, while in the second case the three sections moving up are animated over section A.
Finally, it appears that when animating a set of changes that involves some sections that move down and others that move up, the sections that don't move are on the bottom, the sections that move down are above them, and the sections that move up are on top.
UITableView moveSection example.
Inserting, Deleting, and Moving Rows and Sections:
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;
Reloading:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadSectionIndexTitles;
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
Don't forget to put your code inside
[tableView beginUpdates];
//do stuff...
[tableView endUpdates];
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;
Reloading:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadSectionIndexTitles;
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
Don't forget to put your code inside
[tableView beginUpdates];
//do stuff...
[tableView endUpdates];