tarheel
tarheel

Reputation: 4797

tableView:commitEditingStyle:forRowAtIndexPath: causing Assertion failure

I have a tableView showing an NSMutableArray of an object and I am currently trying to implement the tableView:commitEditingStyle:forRowAtIndexPath: method. If I comment out the line [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; everything works just fine, outside of updating the UI.

This is my implementation of the method:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.modelClass removeObjectAtRow:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

This is the error I get:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2280.1/UITableView.m:1060

Other questions have had answers suggesting that I need to replace the @[indexPath] with [NSArray arrayWithObject:indexPath], and that just comes up with the same error.

Upvotes: 1

Views: 5679

Answers (2)

iNoob
iNoob

Reputation: 3364

This comes because UITableView enters the datasource method numberOfRowsInSection again, after the deletion of rows. Make sure that it returns correct number of rows that is your self.modelClass.count in numberOfRowsInSection.

Upvotes: 2

Kanan Vora
Kanan Vora

Reputation: 2122

Might be the problem is in method numberOfRowsInSection So, to leave troubles you should:

In function commitEditingStyle delete data from your array, database etc. Decrement your current row count.

[tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

And your problem will be solved!!!

Upvotes: 3

Related Questions