【发布时间】:2018-11-14 09:32:53
【问题描述】:
我构建了一个表格视图,使用 FRC 来显示来自核心数据的数据。我还实现了 controller didChange 委托,以查看核心数据中的更改:
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch (type) {
case .insert:
if indexPath != nil && (indexPath?.row)! < (controller.fetchedObjects?.count)!{
tableView.insertRows(at: [indexPath!], with: .left)
}
break;
case .update:
if indexPath != nil && (indexPath?.row)! < (controller.fetchedObjects?.count)!{
self.tableView.reloadRows(at: [indexPath!], with: .left)
}
break;
case .delete:
if indexPath != nil && (indexPath?.row)! < (controller.fetchedObjects?.count)!{
self.tableView.deleteRows(at: [indexPath!], with: .left)
}
break;
case .move:
if indexPath != nil && (indexPath?.row)! < (controller.fetchedObjects?.count)!{
print("type.move: shouldn't get in here!")
}
break;
}
}
当我运行时,我在控制台上得到了这个:
*** -[UITableView _endCellAnimationsWithContext:] CoreData 中的断言失败:错误:严重的应用程序错误。捕获到异常 在调用期间从 NSFetchedResultsController 的委托 -controllerDidChangeContent:。无效更新:第 0 节中的行数无效。现有节中包含的行数 更新后(216)必须等于包含的行数 在更新之前的那个部分(215),加上或减去的数量 从该部分插入或删除的行(0 插入,0 删除)和 加或减移入或移出该部分的行数(0 迁入,0迁出)。与 userInfo (null)
虽然应用程序没有崩溃 - 但 tableview 被搞砸了并且不显示日期。
【问题讨论】:
-
似乎您已经通过控制器更新了数据(模型),但您没有更新视图。如果您在委托方法
controllerDidChangeContent中查看您的代码以帮助识别问题,我会很有用。
标签: swift uitableview core-data delegates nsfetchedresultscontroller