-reloadData:?


#1

In the OS X book, when wanting to add a row to a tableview, we were taught to add it to the datasource’s array, and then send -reloadData: to the tableview. In the iOS book, in this chapter, we are taught to insert a row in the actual tableview by sending -insertRowsAtIndexPaths:, and also first add the data to the datasource’s array. Why the different approaches?

I did read the iOS doc, and it does warn no to use -reloadData: when inserting (or deleting) rows in a tableview; however, it does not go on to say why.

Just wondering, for my own edification. Thanks.


#2

Just a guess:
Reloading the whole table View needs much more resources than just updating one single cell. If the dataSource keeps the data consistent, it only needs to ask the datasource for this single inserted cell, so it kind of trusts it. Same with deleting: It doesn´t even ask for any confirmation. It just animates and trusts the data source to do a good job by deleting exactly the item that it deletes visually.

Try to delete a wrong item in the commitEditingStyle method like this:

[code]-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
BNRItemStore *store = [BNRItemStore sharedStore];
[store removeItem:[[store allItems]objectAtIndex:indexPath.row-1]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

}[/code]

What happens: The tableView itself deletes the correct cell visually, the dataSource the wrong one in the model. The cells in the tableView are not being updated (I think for performance reasons)

EDIT: if you delete the first one, it crashes, of course. And: Not even moving after deleting a row in this way updates anything. The tableView just keeps the cell like it is and tells the controller to move everything same way in the model. It completely ignores the inconsistence.


#3

[quote=“wagill”]In the OS X book, when wanting to add a row to a tableview, we were taught to add it to the datasource’s array, and then send -reloadData: to the tableview. In the iOS book, in this chapter, we are taught to insert a row in the actual tableview by sending -insertRowsAtIndexPaths:, and also first add the data to the datasource’s array. Why the different approaches?

I did read the iOS doc, and it does warn no to use -reloadData: when inserting (or deleting) rows in a tableview; however, it does not go on to say why.

Just wondering, for my own edification. Thanks.[/quote]

I had similar question. Whole code in the addNewItem method in the Adding Rows chapter, could be simplified like this:

[code]- (IBAction)addNewItem:(id)sender
{
[[BNRItemStore sharedStore] createItem];
[self tableView] reloadData];

}
[/code]
seems simpler to me… instead of calling insertRowsAtIndexPaths etc…