2012-04-03 72 views
0

我遇到了insertRowsAtIndexPaths:的问题。我不太确定它是如何工作的。我观看了WWDC 2010上的视频,但我仍然收到错误消息。我以为我应该更新模型,然后包装insertRowsAtIndexPaths:在tableView beginUpdates和endUpdates调用中。我有这个:insertRowsAtIndexPaths无效的表视图更新:

self.customTableArray = (NSMutableArray *)sortedArray; 
[_customTableView beginUpdates]; 
[tempUnsortedArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    [sortedArray enumerateObjectsUsingBlock:^(id sortedObj, NSUInteger sortedIdx, BOOL *sortedStop) { 
     if ([obj isEqualToString:sortedObj]) { 
      NSIndexPath *newRow = [NSIndexPath indexPathForRow:sortedIdx inSection:0]; 
      [_customTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newRow] withRowAnimation:UITableViewRowAnimationAutomatic]; 
      *sortedStop = YES; 
     } 
    }]; 
}]; 
[_customTableView endUpdates]; 

customTableArray是我的模型数组。 sortedArray只是该数组的排序版本。当我点击我的加号按钮添加新行时,运行此代码时,出现此错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (2 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

我不确定我在做什么错。思考?谢谢。

回答

3

我建议你看看阵列中实际发生的情况。当错误读出时,你告诉表视图添加两行出于某种原因,而它说它“有一行”,然后在调用-endUpdates方法之后检查数据源,并且数组总共只有两个对象,而不是三个。

从本质上讲,你的枚举是拾取两个插入。你的数组有两个对象。该表已经有一个对象。 1个现有的+2个插入= 3个行。你的数组中只有两个当前对象。发生什么事额外的对象。

我期望在某个地方,或者你的两个数组不是同步的,或者你评估哪个插入的方式有某种形式的错误。

我希望有帮助。