Tableview更新'NSInternalInconsistencyException',原因:'无效更新:无效行数? [英] Tableview update 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows?

查看:118
本文介绍了Tableview更新'NSInternalInconsistencyException',原因:'无效更新:无效行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的数据源方法,

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

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

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

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.

推荐答案

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

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

这篇关于Tableview更新'NSInternalInconsistencyException',原因:'无效更新:无效行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆