从TableView和coreData中删除行 [英] Deleting Row from TableView and coreData

查看:135
本文介绍了从TableView和coreData中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从填充了coreData对象的tableView中删除行有一些问题。

I have some problem with deleting rows from a tableView which is filled with coreData objects.

尝试这样做:

 -(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];       
        }   
    }



因此,当我点击删除该应用程序崩溃与以下日志:

So, when I click the "Delete" button, the app crashes with the following Log:

    *** 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]之后添加以下行:

I also tried to add the following line after [self.managedObjectContext save:nil]:

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

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

But the app crashes like before.

另一件事,每次我通过删除操作崩溃后重新启动应用程序,应该删除的单元格真的走了!

One more thing, everytime i start the app again after a crash through delete-action, the cell which should be deleted is really gone!

会很好,如果有人可以帮助。
我知道有很多关于类似问题的问题,我尝试了不同的东西从这些,没有成功。
谢谢!

Would be very nice, if somebody can help. I know there are many questions about similar problems and I tried different things from these, without success. Thanks!

推荐答案

表视图由获取的结果控制器委托自动更新
方法

The table view is updated automatically by the fetched results controller delegate methods when objects are added, deleted or modified.

因此,要删除对象,只需从受管对象上下文中删除它。
不要调用 beginUpdates endUpdates deleteRowsAtIndexPaths performFetch

Therefore, to remove an object, just delete it from the managed object context. Don't call beginUpdates, endUpdates, deleteRowsAtIndexPaths or performFetch:

-(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];
    }
}

这篇关于从TableView和coreData中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆