objective-c 如何使 deleteRowsAtIndexPaths: 与 GenericTableViewController 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1016200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make deleteRowsAtIndexPaths: work with GenericTableViewController?
提问by mmc
I'm using Matt Gallagher's GenericTableViewControlleridea for controlling my UITableViews. My datasource is a NSFetchedResultsController.
我正在使用 Matt Gallagher 的GenericTableViewController想法来控制我的UITableViews. 我的数据源是一个NSFetchedResultsController.
http://cocoawithlove.com/2008/12/heterogeneous-cells-in.html
http://cocoawithlove.com/2008/12/heterogeneous-cells-in.html
Everything is working fine, until I try to delete a cell.
一切正常,直到我尝试删除一个单元格。
I have the following code in my View Controller:
我的视图控制器中有以下代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object.
NSManagedObjectContext *context = [wineryController managedObjectContext];
[context deleteObject:[wineryController objectAtIndexPath:indexPath]];
NSError *error;
if (![context save:&error]) {
// Handle the error.
}
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
The final line crashes with the rather verbose explanation in the console:
最后一行因控制台中相当冗长的解释而崩溃:
*** 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 (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
OK, I understand what it is saying... a row is not getting deleted (I would assume) because I'm not forwarding some message to the right place (since I have moved some code from its 'normal' location)... anyone have any idea which one? I am totally stumped on this one.
好的,我明白它在说什么......一行没有被删除(我假设),因为我没有将一些消息转发到正确的位置(因为我已经从它的“正常”位置移动了一些代码)。 . 有谁知道是哪一个?我完全被这个难住了。
回答by mmc
Well, bah. I just found thisanswer, which is not the same, but got me headed in the right direction. I'll leave this here for anyone in the future having similar troubles.
嗯,呸。我刚刚找到了这个不一样的答案,但让我朝着正确的方向前进。我会把这个留在这里给将来遇到类似问题的人。
The key is to wrap the deleteRowsAtIndexPaths with begin and end tags, and force the model to update within the same block, resulting in:
关键是用开始和结束标记包装 deleteRowsAtIndexPaths,并强制模型在同一块内更新,导致:
[tableView beginUpdates];
[self constructTableGroups];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
This caused the issue to go away, and the animations to work just perfectly.
这导致问题消失,动画完美运行。

