Monday, November 16, 2009

'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section

This error throws when commitEditingStyle , Usually this exception is appearing when build with >= 3.0 level

When we remove data from sections, and if the section is empty , we have to remove the section also

following is the sample code:
This error throws when commitEditingStyle , Usually this exception is appearing when build with >= 3.0 level

When we remove data from sections, and if the section is empty , we have to remove the section also

following is the sample code:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// modelForSection is a custom model object that holds items for this section.
[modelForSection removeItem:[self itemForRowAtIndexPath:indexPath]];

[tableView beginUpdates];

// Either delete some rows within a section (leaving at least one) or the entire section.
if ([modelForSection.items count] > 0)
{
// Section is not yet empty, so delete only the current row.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
else
{
// Section is now completely empty, so delete the entire section.
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
withRowAnimation:UITableViewRowAnimationFade];
}

[tableView endUpdates];
}
}
courtesy://http://stackoverflow.com/questions/1061071/uitableview-deleting-sections-with-animation


this is the error message I got
'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'

In my case the above solutions didn't work.. :(
I got this error when build the existing source with sdk 3.0 and above..My previous build level was 2.0. Now I want to shift to 3.0 ..

Solved by the following way

The error message describes that, the no.of rows should be same, after delete one row..
It means if you have only one row, and you delete that row, then the remaining row count should be zero

I have used a display like "No Contacts" when my list is empty, to implement this I added an array with size 3 and show that (No Contacts) when ever list is empty

so this is the corrected code in commitEditing style implementation

[arrayData removeObjectAtIndex:indexPath.row];
[tableView beginUpdates];
//this flag is used to check in numberOfRowsinSection(),
//the code would be
// if [arrayData count] == 0 && this flag is true, reset flag and return 0;

// if [arrayData count] == 0 && this flag is false return the array with size 3 to display "No //Contacts"
self.isRowDeleted = YES;
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

//this is used to
display "No Contacts"
if ([arrayData count] == 0 ){
[tableView reloadData];
}

No comments:

Post a Comment