2013-02-18 71 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]; 
} 
相關問題