Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Delete, Insert & Move ordering in batch updates

Tags:

uitableview

UITableView updates between beginUpdates and endUpdates calls are batched together and all performed at the same time. Apple's documentation is specific about the order in which insert and delete operations are performed:

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. Inserting or removing an item in a mutable array, on the other hand, may affect the array index used for the successive insertion or removal operation; for example, if you insert an item at a certain index, the indexes of all subsequent items in the array are incremented.

It's also worth noting that:

Calls to beginUpdates and endUpdates can be nested; all indexes are treated as if there were only the outer update block.

[Emphasis mine]

So: think about deletions as occurring in a first pass, and then any insertions happening following this, using the new index paths that result after the deletions.

That's useful. However, I've not found any documentation about where row (and section) moves occur, which indexing they should use, and whether this impacts on the other steps. Anyone know?

like image 773
Benjohn Avatar asked Feb 25 '15 16:02

Benjohn


People also ask

How do you delete a row in Swift?

Enter Swift as Language and choose Next. Go to the Storyboard and Remove the View Controller. Drag a Navigation Controller from the Object Library to the empty canvas, this will also contain a table view controller. When the initial View Controller is deleted there isn't a starting point defined.

How do you insert a row in Swift?

The easiest way to add a new row to a UITableView would be to add a new item to the data and then call the reloadData method on the `UITableview. This will add a new row to our UITableView because we have added mercury to our data and then we called reloadData .


1 Answers

When moving table row, you specify two indexes:

  • indexPath in original table from where to take this row
  • newIndexPath in resulting table where this row will appear

    func moveRow(at indexPath: IndexPath, to newIndexPath: IndexPath)

Possible conflicting operations in single batch, causing app crash:

  • trying to move a row you also deleting
  • trying to move same row to multiple destinations
  • trying to move several rows to the same destination
  • trying to move a row to the same destination where you insert new row

It is not possible to move a row you are inserting, because it was not there in original table.

By default, moved row will be not reloaded from data source.

like image 158
paiv Avatar answered Nov 09 '22 23:11

paiv