Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What order do batched row insertions/deletions take place in UITableView?

UITableView allows you to batch editing operations using beginUpdates and endUpdates.

My question is: do I need to know whether it does deletions or insertions first? Or can I refer to everything by the index path prior to beginUpdates and it'll magically work?

Suppose I have a table:

A (currently index path 0,0)
B (0,1)
C (0,2)
D (0,3)
E (0,4)
F (0,5)

I want to turn it into:

A (0,0)
C (0,1)
D (0,2)
H (0,3)
E (0,4)
F (0,5)

Thus, I've deleted B (which was at 0,1) and inserted H (which was inserted after D — at 0,4 before the deletions, or 0,3 after).

So, between my begin/end updates calls, which of these will work?

  1. deleteRowsAtIndexPaths: 0,1, followed by insertRowsAtIndexPaths: 0,4
  2. deleteRowsAtIndexPaths: 0,1, followed by insertRowsAtIndexPaths: 0,3
  3. insertRowsAtIndexPaths: 0,4, followed by deleteRowsAtIndexPaths: 0,1
  4. insertRowsAtIndexPaths: 0,3, followed by deleteRowsAtIndexPaths: 0,1
like image 229
Amy Worrall Avatar asked Aug 03 '11 13:08

Amy Worrall


1 Answers

The relevant Apple documentation for this is under Ordering of Operations and Index Paths.

Deletion and reloading operations within an animation block specify which rows and sections in the original table should be removed or reloaded; insertions specify which rows and sections should be added to the resulting table. The index paths used to identify sections and rows follow this model.

So the table view will first perform any delete or update operations, whose index paths refer to index paths in the original table contents. Then insertions are performed, and those index paths refer to index paths after deletions have occured.

So in theory your number '2' option should be the one you want.

like image 144
Mike Weller Avatar answered Sep 27 '22 20:09

Mike Weller