【问题标题】:Core Data Deletion Error核心数据删除错误
【发布时间】:2012-07-12 04:41:08
【问题描述】:

尝试从与获取的结果控制器挂钩的 tableview 中删除行时出现以下错误:

* -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:1037 2012-07-12 13:11:19.921 Chef[28843:12203] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。包含的行数在更新后的现有节中 (20) 必须等于更新前该节中包含的行数 (20),加上或减去从该节插入或删除的行数(0 插入,1 删除)和加上或减去移入或移出该部分的行数(0 移入,0 移出)。'

我的代码如下。

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {

    // Delete the record from the DB
    RecipeCategory *objectToBeDeleted = [self.fetchedResultsController objectAtIndexPath:indexPath];
    [self.context deleteObject:objectToBeDeleted];

    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];


}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}   
}

使用获取请求计数和方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section,我尝试记录数据库记录的数量,以及之前和表中的行数在 [self.context deleteObject:objectToBeDeleted]; 行之后,它们都从 20 减少到 19。

请帮忙!

编辑:PS 错误发生在 deleteRowsAtIndexPath 方法。

【问题讨论】:

  • 你的 numberOfRows 中有什么?
  • - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[[self.fetchedResultsController section] objectAtIndex:section] numberOfObjects]; }
  • 如果交换 deleteRowsAtIndexPathsdeleteObject 方法会发生什么?表格视图可能希望您先删除可见行,然后再删除它们后面的信息。
  • 你在使用NSFetchedResultsController吗?
  • 是的,使用 NSFetchedResultsController,我认为这是一个相当标准的要求。现在只是浏览 Apple 示例应用程序,以了解我在做的不同之处,但到目前为止还没有乐趣。

标签: ios core-data nsfetchedresultscontroller


【解决方案1】:

事实证明,下面的代码行超出了要求。我猜 NSFetchedResultsController 也可以处理这个问题。我还必须实现 NSFetchedResultsController 的委托方法。

// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多