gpt4 book ai didi

ios - 从 TableView 和 coreData 中删除行

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

我在从充满 coreData 对象的 tableView 中删除行时遇到一些问题。

尝试这样做:

 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{

if (editingStyle == UITableViewCellEditingStyleDelete) {

[self.tableView beginUpdates];

NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Deleting (%@)", [managedObject valueForKey:@"original_title"]);
[self.managedObjectContext deleteObject:managedObject];
[self.managedObjectContext save:nil];

[self performFetch];
[self.tableView endUpdates];
}
}

因此,当我单击“删除”按钮时,应用程序崩溃并显示以下日志:

    *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2872.3/UITableView.m:1254
2013-08-13 18:39:17.624 RR_proto[1076:a0b] *** Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.
The number of sections contained in the table view after the update (1) must be equal
to the number of sections contained in the table view before the update (2), plus or minus
the number of sections inserted or deleted (0 inserted, 0 deleted).'
*** First throw call stack:

我还尝试在 [self.managedObjectContext save:nil] 之后添加以下行:

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

但应用程序像以前一样崩溃。

还有一点,每次我通过删除操作崩溃后再次启动应用程序时,应该删除的单元格真的不见了!

如果有人能提供帮助,那就太好了。我知道有很多关于类似问题的问题,我尝试了不同的方法,但没有成功。谢谢!

最佳答案

表格 View 由获取的结果 Controller 委托(delegate)自动更新添加、删除或修改对象时的方法。

因此,要删除一个对象,只需将其从托管对象上下文中删除即可。不要调用 beginUpdatesendUpdatesdeleteRowsAtIndexPathsperformFetch:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.managedObjectContext deleteObject:managedObject];
[self.managedObjectContext save:nil];
}
}

关于ios - 从 TableView 和 coreData 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18215115/

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