The rows in a table are changed in two ways: (1) The underlying data changes and the table has to reload to display the new data. (2) The user edits the table directly by inserting, deleting or dragging. The block handles (2).
The beginUpdates
to endUpdates
block tells the table that it is being directly edited and that it should not rely on the values returned by its datasource. Without the block, the table calls numberOfRowsInSection
as normal to find out how many rows it should have. If the user has just inserted or deleted a row, it will receive and incorrect value because the data model might not yet been updated. The datasource will not return the correct value until after the table displays the change and the delegate writes to the data.
You can think of the block as a kind of temporary decoupling of the table from its data source.