我如何使deleteRowsAtIndexPaths:使用GenericTableViewController? [英] How can I make deleteRowsAtIndexPaths: work with GenericTableViewController?
问题描述
我使用Matt Gallagher的 GenericTableViewController
想法来控制我的 UITableViews
。我的数据源是 NSFetchedResultsController
。
I'm using Matt Gallagher's GenericTableViewController
idea for controlling my UITableViews
. My datasource is a NSFetchedResultsController
.
http://cocoawithlove.com/2008/12/heterogeneous-cells-in.html
一切正常,直到我尝试删除一个单元格。
Everything is working fine, until I try to delete a cell.
我在View Controller中有以下代码:
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];
}
}
最后一行会在控制台:
*** 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.
推荐答案
好吧,bah。我刚刚找到 此 答案,这是不一样的,但让我朝正确的方向前进。我会离开这里为任何人在未来有类似的麻烦。
Well, bah. I just found this answer, 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.
关键是包装deleteRowsAtIndexPaths与开始和结束标签,并强制模型更新同一块,导致:
The key is to wrap the deleteRowsAtIndexPaths with begin and end tags, and force the model to update within the same block, resulting in:
[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.
这篇关于我如何使deleteRowsAtIndexPaths:使用GenericTableViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!