When I try to remove a UITableViewCell, I get this error: Invalid update: invalid number of rows in section 0, but I can't seem to fix it

I have a gesture on a UITableViewCell subclass called ArticleCell, so when it is swiped a method in the UITableViewController class gets called to delete the cell that was swiped.

The delegate method looks like this:

- (void)swipedToRemoveCell:(ArticleCell *)articleCell {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:articleCell];

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];

    [self.tableView reloadData];
}

But every time I swipe, I get this error:

Invalid update: invalid number of rows in section 0

More information: It uses Core Data for the data source, so it uses NSFetchedResultsController. Do I have to update something there? (I haven't touched any of its methods.)

Answers


You always need to also remove the row from your data source object as well. You will need to remove it from your Core Data store at the same time as you delete the row representing the data from the table view itself.

The issue is this mismatch, you remove the row from the table view but your -numberOfRowsInTableView data source method is still returning the old number of rows because the fetched results controller still sees that number in the data store.


Need Your Help

magento Not valid template file /page/1column.phtml

templates magento magento-1.7

I had my site running fine on the devp. server. After I migrated the app to my production server. Everything worked until I added an extension and enabled it. The site still works but the product v...

Catch 22 re Android Market Developer Agreement

android google-play

This is not a programming question per se but a question that may be relevant to all future Android developers.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.