views:

26

answers:

1

Apple says about the UITableView -beginUpdates -endUpdates block:

If you do not make the insertion, deletion, and selection calls inside this block, table attributes such as row count might become invalid.

I'd like to know what exactly -beginUpdates does. Why do table attributes such as row count might become invalid when I don't put my insert/delete code inside this block?

+2  A: 

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.

TechZen