The UITableView documentation says that it’s OK to combine calls to moveSection:toSection:, insertSections:withRowAnimation:, and deleteSections:withRowAnimation: in a beginUpdates–endUpdates block. A section called Batch Insertion, Deletion, and Reloading of Rows and Sections in the Table View Programming Guide also explains that when mixing inserts and deletes inside an update block, the table view does the deletions before it does the insertions no matter what order the method calls are made in.
My question is, when calls to moveSection:toSection: are combined with calls to insertSections: and deleteSections:, in what order does the table view do the moves? Or alternatively, do the fromSection and toSection refer to the section indices before the deletes, between the deletes and inserts, or after the inserts?
After experimenting with some code, it appears that the best way to think about a batch of changes inside a
beginUpdates–endUpdatesblock is terms of the order of the sections before any of the updates are applied and the order of sections after all of the updates are applied. If U represents the order before the update block and V the order after update block, then the section indices used in thedeleteSections:calls and the source indices in themoveSection:toSection:are the indices in U and the section indices used in theinsertSections:and the destination indices in themoveSection:toSection:calls are the indices in V.So, for example, to animate the changes:
you could use:
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:
the following are logically equivalent:
and
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.