ios - 使用核心数据删除带有动画的表格行

标签 ios iphone uitableview delete-row

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

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

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

这是我的代码-感谢您的帮助。这种删除行的方法在iOS4之前有效,不确定是否有所更改?

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

最佳答案

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

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

关于ios - 使用核心数据删除带有动画的表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4276804/

相关文章:

ios - 访问图库中的连拍模式照片

ios - 我想使用 CorePlot 从图形中删除负数部分?

ios - 我在 iOS 中遇到一些布局约束错误

iphone - 防止移动 safari 上的水平滚动,overflow-x :hidden breaks site

iphone - 如何相对于文本标签向 MBProgressHUD 添加按钮?

iPhoneSDK : UIBarButtonItem's selector method targets wrong instances

ios - 在应用程序中集成操作扩展时无法获取 "Choose an app to run"弹出窗口中的所有应用程序

ios - 将具有某些属性的 TableView 中选定单元格的核心数据传递给另一个 VC

ios - 在动态表中重新加载 TableViewCell 的 UILabels

swift - 模型未表示的可见单元格后面的隐藏 TableViews 单元格