When using - (void)beginUpdates
and - (void)endUpdates
on a UITableView do you have to make the changes to the datasource inside calls?
i.e.
If I have a NSMutableArray
called dataSource
driving my tableView
could I do this...
// edit the actual data first
[dataSource addObject:@"Blah"];
// now update the table
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:dataSource.count - 1 inSection:0] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
Or is it necessary to do...
[self.tableView beginUpdates];
[dataSource addObject:@"Blah"];
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:dataSource.count - 1 inSection:0] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
Just asking because I have a couple places where I am updating and I could potentially take the common code out to a function.
But only if I can update outside the update calls.
The tableview is the instance of the UITableView class, which inherits the UIScrollView class.
beginUpdates()Begins a series of method calls that insert, delete, or select rows and sections of the table view.
UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want.
It is generally a good idea to update the datasource in between tableView beginUpdates/endUpdates. After the endUpdates is called, the OS will trigger the dataSource calls to get the sections and row counts. If at that point, the datasource has not been updated, then you may get tableView inconsistency crashes.
It may work in some cases to update the datasource before the tableView beginUpdates/endUpdates call, however you must make sure that you are updating the datasource in the same thread and on the same run loop. Since tableView updates must be done on the main thread, that means you have to make your datasource updates on the main thread.
If you update the datasource, and don't immediately do your tableView updates (and you do something like rotate the screen), then you can get a tableView inconsistency crash.
I have always used 1st approach and it causes no problem. just make sure that after changing data source, immediately update your table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With