Which tableView cells are visible after a batch update?

154 views Asked by At

I am confused about how moves of table view rows in batch updates are to be handled.
I have a tableView that is updated in a batch update block:

tableView.performBatchUpdates {
//…
} 

Within the block, deletes, inserts, updates and moves have to be processed.
The Apple docs say

Deletes are processed before inserts in batch operations. This means the indexes for the deletions are processed relative to the indexes of the table view’s state before the batch operation, and the indexes for the insertions are processed relative to the indexes of the state after all the deletions in the batch operation.

I assume that the indexes of moves and updates relate to the state after deletes and inserts have be done, although this is not explicitly stated.
In my case, I have to configure a table view cell after it has been moved. But there are different cases to be considered:

  1. If a moved row is visible before and after the move, I can access the cell using let cell = tableView.cellForRow(at: IndexPath), and configure it. But which index path is to be used: before or after the move?
  2. If a moved row is visible only before the move, it is not necessary to configure it.
  3. If a moved row is visible only after the move, there is not yet a cell that can be configured.

Of course, I could store which rows have been deleted, inserted and moved until the batch update is finished, and check in the completion block if an inserted or moved row is visible, and configure its cell if so.
Is this the right way to do?

0

There are 0 answers