reloadSection 导致错误“尝试删除第 1 节,但在使用 userInfo 更新之前只有 1 个节”

Dru*_*rux 5 core-data uitableview uikit nsfetchedresultscontroller ios

我收到错误“尝试删除第 1 节,但在使用 userInfo 更新之前只有 1 个节”,这显然是由调用reloadSections:withRowAnimation:. 这是怎么发生的,因为重新加载不是删除,或者是部分删除?

当回滚 a 中的更改(UITableViewController其数据源部分由 a 支持)时会发生这种UIFetchedResultsController情况(表的第一个部分包含常量行;其其余部分对应于 sUIFetchedResultController部分:在本例中,有两个这样的部分,每部分一行。UITableViewController还担任 的UIFetchedResultsController代表。

在回滚期间(以取消删除最后两个部分),委托会收到以下调用:

  1. controllerWillChangeContent:; numberOfSectionsInTableView是 1;代表通话beginUpdates
  2. controller:didChangeSection:atIndex:forChangeType:宣布NSFetchedResultsChangeInsert第 1 条;numberOfSectionsInTableView is 3; delegate callsinsertSections:withRowAnimation: for section 2,reloadSections:withRowAnimation:` 第 1 节
  3. controller:didChangeSection:atIndex:forChangeType:宣布NSFetchedResultsChangeInsert第 0 节;代表呼吁insertSections:withRowAnimation:第 1 节,reloadSections:withRowAnimation:第 0 节
  4. controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:宣布NSFetchedResultsChangeInsertindexPath(1,0);代表要求insertRowsAtIndexPaths:withRowAnimation:( indexPath2,0)
  5. controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:宣布NSFetchedResultsChangeInsertindexPath(0,0);代表要求insertRowsAtIndexPaths:withRowAnimation:( indexPath1,0)
  6. controllerDidChangeContent:; 委托调用endUpates,这会导致此错误:

尝试删除第 1 节,但在更新 userInfo 之前只有 1 节

如果委托没有reloadSections:withRowAnimation按照指示进行调用,则不会发生错误。

在这种特定情况下,是什么原因导致错误以及如何防止它?(顺便说一句,在这种情况下,需要重新加载才能更新相邻部分中页眉和页脚的标题。)

更新我认为问题可能与以下事实有关:在一个步骤中numberOfSectionsInTableView从 1 跳回到 3 (由于) ,而实际部分(和行)的数量在和之间的“括号”内逐渐增长。但是,括号不正是为了允许数据源中的项目数与表中的项目数之间存在临时差异吗?我的定义如下:rollbackcontrollerWillChangeContent:controllerWillChangeContent:controllerDidChangeContent:numberOfSectionInTableView:

- (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView {
    return 1 + self.fetchedResultsController.sections.count;
}
Run Code Online (Sandbox Code Playgroud)