2013-10-14 103 views
5

我解析XML文件,並把它的分析值在表view.The表視圖只有一個section.But我得到以下異常:斷言失敗 - [UITableView的_endCellAnimationsWithContext]

2013-10 -14 15:21:57.250 tableview [6068:907] *聲明失敗 - [UITableView _endCellAnimationsWithContext:],/ SourceCache /UIKit/UIKit-2380.17/UITableView.m:909 2013-10-14 15:22:11.227 tableview [6068:907] 由於未捕獲的異常'NSInternalInconsistencyException'而終止應用程序,原因是:'試圖將0行插入到0節中,但更新後0節中只有0行' * *第一擲調用堆棧: (0x33e752a3 0x3bcde97f 0x33e7515d 0x3474aab7 0x35cb0a9f 0x35de6b27 0x6a10f 0x69ce5 0x33dc6037 0x346dc599 0x6b54b 0x3478c0f5 0x33e4a683 0x33e49ee9 0x33e48cb7 0x33dbbebd 0x33dbbd49 0x379702eb 0x35cd1301 0x68bdd 0x68b64) 的libC++ abi.dylib:終止叫做拋出異常

的代碼是:

/** 
The NSOperation "ParseOperation" calls addFiles: via NSNotification, on the main thread which in turn calls this method, with batches of parsed objects. The batch size is set via the kSizeOfEarthquakeBatch constant. 
*/ 
- (void)addFilesToList:(NSArray *)files { 

    NSInteger startingRow = [self.fileList count]; 
    NSLog(@"STARTING ROW:%d",startingRow); 
    NSInteger fileCount = [files count]; 
    NSLog(@"FILE COUNT : %d",fileCount); 
    NSMutableArray *indexPaths = [[NSMutableArray alloc] initWithCapacity:fileCount]; 
    NSLog(@"INDEX PATHS:%@",indexPaths); 
    for (NSInteger row = startingRow; row < (startingRow + fileCount); row++) { 
     NSLog(@"into for"); 

     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0]; 
     NSLog(@"INDEXPAth %@",indexPath); 
     [indexPaths addObject:indexPath]; 
    } 

    [self.fileList addObjectsFromArray:files]; 
    NSLog(@"ADD objects"); 
    NSLog(@"FILELIST:%@",self.fileList); 

    //GETTING EXCEPTION AT THIS LINE 

    [self.tableview insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 


#pragma mark - UITableViewDelegate\ 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

// The number of rows is equal to the number of earthquakes in the array. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [self.fileList count]; 
    NSLog(@"NUMBER OF ROWS :%d",[self.fileList count]); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *kFileCellID = @"Cell"; 
    myTableCell *cell = (myTableCell *)[tableView dequeueReusableCellWithIdentifier:kFileCellID]; 

    // Get the specific earthquake for this row. 
    fileList *file = (self.fileList)[indexPath.row]; 

    [cell configureWithFile:file]; 
    return cell; 
} 

回答

1

在我的情況下,問題是我使用了兩個indexPath(indexPath,lastIndexPath)。但對於lastIndexPath.row已超出我的數組bound.solve通過檢查condition.hope這有助於某人。

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, lastIndexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic]; 
+0

@Megha。謝謝。 –

12

正在發生的事情是,你在添加新的細胞,其實現代碼如下仍然不知道做了self.fileList陣列的變化。 UIKit將添加新單元格並檢查段計數的順序不幸無法控制。 但是爲了解決這個問題,UItableview插入和刪除操作需要被包裝在startsUpdate和endsUpdate方法上。

[self.tableView beginUpdates]; 

// Do all the insertRowAtIndexPath and all the changes to the data source array 

[self.tableView endUpdates]; 
+0

謝謝你 - 這並獲得成功的我! – mdebeus

0

我在更新API調用後的行。

嘗試更新主線程內的行。 (因爲所有UI更新都應該在主線程內完成)。

試試這個....

DispatchQueue.main.async { 
    self.tableView.insertRows(at: [newIndexPath], with: .bottom) 
}