Vishal Singh
Vishal Singh

Reputation: 4480

UITableViewDataSource methods not getting called

I am updating my UITableView with the following code:

int index = [self.guests indexOfObject:_guests];

[[self tableView] beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
[[self tableView] endUpdates];

But after calling [[self tableView] endUpdates], its not calling its datasource methods. In case of adding row, it calls its datasource methods. I think in case of deletion it does not need to ask anything to its datasource, but in case of adding a row it needs to ask its datasource about pretty much everything about the new row added like cellForRow, height etc etc. I just want to make sure that is it right if deleteRowsAtIndexPaths is not calling any of its datasource methods??

Upvotes: 1

Views: 587

Answers (2)

Lithu T.V
Lithu T.V

Reputation: 20021

The answer is a question " why should it call all the datasource and delegate methods if the view is already popuated and all cells are readily available"? well it Should be the behavior

Upvotes: 0

Adil Soomro
Adil Soomro

Reputation: 37729

deleteRowsAtIndexPaths:indexPaths doesn't call delegate methods in all situation. It just simply deletes rows, If the rows being deleted are so many that they are occupying screen area, then it may call your delegate method. You have to explicitly call reloadData so that it refreshes its rows.

But calling reloadData immediately will spoil your animation or produce weird errors, since its rows are being deleted and you call reload method. (It may go crazy). The alternate solution is to call reloadData after a slight delay like 0.2 or greater like this:

[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.2]; //calling reloadData after a short delay. 0.2 or whatever suits you.

Don't forget about deleting data from your datasource's array or whatever you are using to hold data.

Upvotes: 2

Related Questions