使用核心数据删除带有动画的表格行 [英] Deleting table row with animation using core data

查看:53
本文介绍了使用核心数据删除带有动画的表格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 UITableViewRowAnimationFade 这样的标准动画从表中删除一行,则代码会因以下错误而崩溃:

If I delete a row from my table using a standard animation such as UITableViewRowAnimationFade the code crashes with this error:


***由于未捕获的异常
而终止应用程序'NSInternalInconsistencyException',
原因:'无效的更新:无效的
第1节中的行数。
更新之后
现有部分中包含的行数(4)
必须等于
更新之前该部分中包含的
行数( 4),从该
节中插入或删除的行数
(已插入0个,删除了1个),加或减。'

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'

如果我删除动画行,则行删除/删除有效,但是我不再具有炫酷的Apple Delete过渡。

If I remove the animation line the row removal/deletion works, but I no longer have the cool Apple delete transition.

这是我的代码-任何帮助表示赞赏。这种删除行的方法在 iOS4 之前有效,不确定是否有所更改吗?

Here is my code - any help is appreciated. This method of deleting rows worked prior to iOS4, not sure if something has changed?

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

    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
        MyItem *myItem = [self.getItemsFetchedResultsController objectAtIndexPath:path];
        [dataRepository deleteItem:myItem];

        // If I comment this line out the delete works but I no longer have the animation
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [self.tableView reloadData];

    }

}

数据存储库

-(void)deleteItem:(MyItem *)myItem {
    [self.managedObjectContext deleteObject:myItem];
    [self saveCurrentContext];
}


推荐答案

我通过使用解决了此问题在表更新前后的这段代码:

I resolved this issue by using this code before and after the table update:

[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
    ... 
}
[tableView endUpdates];

这篇关于使用核心数据删除带有动画的表格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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