我遇到了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).'
我不確定我在做什麼錯。思考?謝謝。