gpt4 book ai didi

ios - NSFetchedResultsController:删除 1 行使应用程序崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:13 25 4
gpt4 key购买 nike

从 NSFetchedResultsController 提供的 UITableView 中删除一行会导致我的应用程序崩溃。

错误是:

* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:1330
*
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 (1) 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 (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

我只想滑动删除。我的删除代码是这样的:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView beginUpdates];
SequenceData *sd = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.managedObjectContext deleteObject:sd];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}

我的 NSFetchedResultsController 的设置就像 Ray Wanderlich 的教程 ( http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller ) 中一样

行数由以下因素决定:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    
id sectionObjects = [[self.fetchedResultsController sections] objectAtIndex:section];
NSInteger nbObjects = [sectionObjects numberOfObjects];
return nbObjects;
}

看起来提取没有更新(行数没有变化)。我是否需要自己获取(这不是由获取 Controller 处理的)吗?

这里显然缺少一些基本的东西。不要犹豫,提出基本答案。

我首先用 Controller 实现了这个:didChangeObject: ... 方法。错误是一样的,但一些细节不同。

编辑我相信我的问题已经解决了。这两个答案(来自 CX 和 Martin)帮助我找到了它。 Martin 得到了答案,因为解释帮助我更好地理解了一点......

最佳答案

不要打电话

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

如果您使用获取的结果 Controller 。仅删除带

的对象
[self.managedObjectContext deleteObject:sd];

获取的结果 Controller 委托(delegate)方法 didChangeObject: 然后被自动调用,并调用 deleteRowsAtIndexPaths:

所以在你的例子中,该行被删除了两次,这导致了异常。

请注意,此处不需要beginUpdates/endUpdates

关于ios - NSFetchedResultsController:删除 1 行使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21222840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com