使用fetchedResultController删除表视图中的行 [英] Delete row in table view with fetchedResultController

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

问题描述

在swype删除期间(此方法的大多数importatnt行):

During swype deleting (most importatnt lines of this method):

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
 if (editingStyle == UITableViewCellEditingStyleDelete)
  {
    Table *deleteRow = [self.fetchedResultsController objectAtIndexPath:indexPath];
    [self.managedObjectContext deleteObject:deleteRow];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  }   
}

删除行时出现此错误:


由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第2部分中的行数无效。

更新(1)后现有部分中包含的行数必须等于更新前的该部分中包含的行数(1),加上或减去从该部分插入或删除的行数(插入0) ,1删除)并加上或减去移入或移出该部分的行数(0移入,0移出)。'

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 2.
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).'

如果我评论最后一行代码( [tableView deleteRowsAtIndexPaths:...] ),一切正常(但我必须刷新视图才能看到该行被删除) 。

If I comment last line of code ([tableView deleteRowsAtIndexPaths:...]) everything works fine (but I have to refresh view to see that row was deleted).

如何正确地做到这一点..?

How to do it properly..?

编辑:
考虑@Kyr Dunenkoff的回应我添加了:

Considering @Kyr Dunenkoff responce I've added:

- (void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath
{
    UITableView *tableV = [self tableView];
    switch(type) {
        case NSFetchedResultsChangeDelete:
            [tableV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController*)controller
{
    [[self tableView] endUpdates];
}

- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller
{
    [[self tableView] beginUpdates];
}

然而,这并没有改变崩溃和放大错误。 Atm它只是导致添加新行不再起作用。

However this didn't change the crashing & error. Atm it just caused that adding new rows isn't working any more.

推荐答案

要正确执行此操作,请执行 controller:didChangeObject:方法,在其中为 NSFetchedResultsChangeDelete 开关 $ c> case插入你的 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 。因为当您使用NSFetchedResultsController等数据源时,所有更改都必须来自那里,而您的表只反映它们。
此外,你可以实现 controller:willChangeContent controller:didChangeContent ,把 [tableView beginUpdates] 进入 willChange [tableView endUpdates] 进入 didChange

To do this properly, implement controller:didChangeObject: method, within it make a switch for different change types, within NSFetchedResultsChangeDelete case insert your [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];. Because when you work with data source like NSFetchedResultsController, all changes must come from there and your table only reflects them. Additionally, you can implement controller:willChangeContent and controller:didChangeContent, put [tableView beginUpdates] into willChange and [tableView endUpdates] into didChange.

这篇关于使用fetchedResultController删除表视图中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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