Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tableview update 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows?

Yes there are so many question ask on stack overflow on similar title of question. I read those all but i never see my problem equivalent to them. Now my problem is if when i update table section 0 having 3 rows on click of headerview on section 0 and then again i click on same header for delete all 3 rows from the section 0, this work fine with me. But if i open(update section 0 with 3 rows) and then i click on another header section (another section i want to open then after 0th section) my application crashed. I mean i want to if i click other section then my other section should have open and previous open section should have to close. see my code for insertion and deletion sections and rows,

 -(void)sectionHeaderView:(TableHeaderView*)sectionHeaderView sectionOpened:(NSInteger)section{

self.sectionOpen = YES;
//Create an array containing data of rows and section which to be inserted in tableView.
NSMutableArray *dataInsertArray = [NSMutableArray arrayWithArray:[self.tableDataSourceDictionary objectForKey: [self.sortedKeys objectAtIndex:section]]];
NSInteger countOfRowsToInsert = [dataInsertArray count];

NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < countOfRowsToInsert; i++) {
    [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:section]];
}
[self.tableContents setObject:dataInsertArray forKey:[self.sortedKeys objectAtIndex:section]];

//Create an array containing data of rows and section which to be delete from tableView.
NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
NSInteger previousOpenSectionIndex = self.openSectionIndex;
if (previousOpenSectionIndex != NSNotFound )  {

    self.sectionOpen = NO;
    [previousTableHeader toggleOpenWithUserAction:NO];
    NSInteger countOfRowsToDelete = [[self.tableContents objectForKey: [self.sortedKeys objectAtIndex:section]] count];
    for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
        [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:previousOpenSectionIndex]];
    }

    [self.tableContents removeObjectForKey:[self.sortedKeys objectAtIndex:previousOpenSectionIndex]];
}

// Apply the updates.
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

self.openSectionIndex = section;
self.previousTableHeader = sectionHeaderView;
}

And my data-source methods,

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSInteger numberOfSection = [self.sortedKeys count];
return numberOfSection;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *listData =[self.tableContents objectForKey: [self.sortedKeys objectAtIndex:section]];
NSInteger numberOfRows = [listData count];
return numberOfRows;
}

My crash report, Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:1070 2013-02-18 11:44:49.343 ManageId[1029:c07] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Simply Suppose my 2 sections has contain 3 - 3 rows then it will worked file But if 2 sections contain 3 - 2 rows then it crash. I want to toggle two sections click on updating header of section with inconsistent numbers of rows in those sections.

Than

like image 547
Tirth Avatar asked Feb 18 '13 07:02

Tirth


1 Answers

I found that please replace that code in your code with that code. you have to delete data with old one index not with current one.... the commented line was yours please check that...

if (previousOpenSectionIndex != NSNotFound){
 self.sectionOpen = NO; 
 [previousTableHeader toggleOpenWithUserAction:NO];

//NSInteger countOfRowsToDelete = [[self.tableContents objectForKey: [self.sortedKeys objectAtIndex:section]] count];
NSInteger countOfRowsToDelete = [[self.tableContents objectForKey: [self.sortedKeys objectAtIndex:previousOpenSectionIndex]] count];   

for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
[indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:previousOpenSectionIndex]];
 }

[[self.tableContents objectForKey: [self.sortedKeys objectAtIndex:previousOpenSectionIndex]] removeAllObjects];  
}
like image 175
Deepak Bhati Avatar answered Oct 04 '22 22:10

Deepak Bhati