我遇到了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).'
我不确定我在做什么错。思考?谢谢。