2013-02-18 67 views
8

是的,在类似标题的问题上有太多的问题要问堆栈溢出。我读了所有这些,但我从来没有看到我的问题等同于他们。 现在我的问题是,如果当我更新表部分0有3行点击headerview第0部分,然后再次点击相同的标题删除部分0的所有3行,这与我工作很好。但是,如果我打开(更新0行3行),然后我点击另一个标题部分(另一部分,我想打开然后第0节后)我的应用程序崩溃。我的意思是我想如果我点击其他部分,那么我的其他部分应该有开放和以前开放部分应该关闭。看到我的代码插入和删除部分和行,Tableview更新'NSInternalInconsistencyException',原因:'无效的更新:无效的行数?

-(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行,然后它崩溃。我想切换两个部分,在这些部分中单击更新不一致行数的部分标题。

+0

先尝试删除行和结束的更新然后插入新行update.Or使用reloadData方法,它会根据数据源值自动添加删除行。 – fibnochi 2013-02-18 08:54:58

+0

@fibnochi,我已经做了所有的事情。但没有成功。 – Tirth 2013-02-18 08:58:52

+0

尝试插入,然后删除行。通过这种方式,行数将首先被添加,然后被删除。 – fibnochi 2013-02-18 09:01:15

回答

4

我发现,请更换与代码的代码的代码。你必须删除与旧的索引数据并不符合当前的一个.... 的注释行是你请检查...

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]; 
} 
相关问题