Tableview更新’NSInternalInconsistencyException’,原因:’无效更新:无效行数?

是的,在类似的问题标题上有很多关于堆栈溢出的问题。 我读了所有这些,但我从来没有看到我的问题等同于他们。 现在我的问题是,如果我在第0部分点击headerview更新表格部分0有3行,然后再次点击相同的标题从0部分删除所有3行,这对我来说很好。 但是,如果我打开(更新第3部分有3行),然后我点击另一个标题部分(我要打开的另一部分,然后在第0部分后)我的应用程序崩溃了。 我的意思是我想如果我点击其他部分然后我的其他部分应该打开,之前的开放部分应该关闭。 看到我的代码插入和删除部分和行,

-(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; } 

和我的数据源方法,

 - (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; } 

我的崩溃报告, Assertion失败 – [UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit_Sim / UIKit-2372 / UITableView.m:1070 2013-02-18 11:44:49.343 ManageId [1029:c07]由于未捕获的exception而终止应用程序’NSInternalInconsistencyException’,原因:’无效更新:第0节中的行数无效。更新后的现有部分中包含的行数(0)必须等于更新前该部分中包含的行数(3)加上或减去从该部分插入或删除的行数(0插入,1删除)和加或减移入或移出该部分的行数(0移入,0移出)。

简单地假设我的2个部分包含3到3行然后它将工作文件但是如果2个部分包含3 – 2行然后它崩溃。 我想切换两个部分点击更新部分的标题,这些部分中的行数不一致。

我发现请用您的代码替换代码中的代码。 你必须删除旧的一个索引而不是当前的索引数据….评论的行是你的请检查…

 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]; }