1

For UITableViews, the order of insertion, deletion and move operations does not matter inside an update block because they are not performed until -endAnimations is called, and then, they are performed as a batch update.

Let's say I have 3 arrays:
- deletions (just an index)
- insertions (index + object)
- moves (two indices)

How would I properly apply this 'delta' to an existing array, just like UITableView would in its batch updates?

Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83

1 Answers1

2

The trick is that you keep a dictionary (or array, actually) interpreting the index numbers in terms of what happens during each change. Originally 0 means 0, 1 means 1, and so on. But if you delete item 1, let's say, then 2 means 1, 3 means 2, etc. - everything above it has to be shoved down, so that if you ask to interchange item 2 with something we can still find the object you meant by "2" at the time you said it. Similarly if you then insert something at item 2, everything above it has to be shoved up.

Now you can do the deletions, then the insertions, then the moves. Note that you have to do the deletions in reverse sorted index order (or use an index set so you can delete them all at once) and the insertions in normal sorted order.

matt
  • 515,959
  • 87
  • 875
  • 1,141