Crash when deleting row - Inconsistency in section not being updated
When I delete a row from UITableView using commitEditingStyle, my app crashes with the following error message. The odd thing though is I am deleting from section 3. The inconsistency according to the message is from section 4.
* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1262.60.3/UITableView.m:920 2010-11-22 19:56:44.789 bCab[23049:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 4. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).'
In datasource, I update section 4 depending on the number of rows in section 3. When a row is deleted from section 3, number of rows in section 4 goes from 0 to 1. This seems to cause the issue. Is there no way to avoid this?
Any pointers would be highly appreciated. Thanks.
UPDATE:
numberOfSectionsInTableView - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 6;
}numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
bCabAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
if (section == 0) { // First name, last name
return 2;
}
else if (section == 1) { // Password
return 2;
}
else if (section == 2) { // Mobile, DOB , Gender
return 3;
}
else if (section == 3) { // credit cards
return [creditCards count];
}
else if (section == 4) { // Add credit card
if ([creditCards count] >= 3) {
return 0;
}
else {
return 1;
}
}
else if (section == 5) {
return 0;
}
return 0;
}
commitEditingStyle
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)in开发者_JAVA技巧dexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (indexPath.section == 3) {
// Delete the row from the data source
NSLog(@"%d %d", indexPath.section, indexPath.row);
[creditCards removeObjectAtIndex:indexPath.row];
// Delete from backend
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
//[tableView reloadData];
}
}
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
}
}
I have tried with and without [tableView reloadData] with same results.
Thanks for trying to help!
In datasource, I update section 4 depending on the number of rows in section 3. When a row is deleted from section 3, number of rows in section 4 goes from 0 to 1. This seems to cause the issue. Is there no way to avoid this?
When using deleteRowsAtIndexPaths:withAnimation
you are guaranteeing that the data source will have removed just the rows with the specified index paths. In your case you are also inserting a row into the table which means that state of the data source is not what the table view expects.
When deleting a row in section 3 that also involves inserting a row in section 4 you must do something like:
[self.tableView beginUpdates];
[self.tableView [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPath:[NSArray arrayWithObject:indexPathForInsertedRow] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
Before deleting the cell, do you remove the corresponding object in creditCards ?
How do you add the new row in section 4 when a row in section 3 is deleted ?
It is not a good solution, but you can try to make a reloadData when row is deleted.
精彩评论