ios - Swift 2 核心数据 : delete object ok! 但 TableView 中没有刷新行

标签 ios uitableview core-data swift2

此代码工作正常,在核心数据中删除。 TableView 将 DELETE 设置为右侧红色,但不删除 TableView 中的行。

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

    let manageObject: NSManagedObject = frc.objectAtIndexPath(indexPath) as! NSManagedObject
moc.deleteObject(manageObject)

    do {
        try moc.save()
    } catch {
        print("Failed to save")
        return
    }
}

如果我停止应用程序然后再次运行它, TableView 不会对已删除的行进行采样,也不会对它们保留的行进行采样。

最佳答案

通过添加显式删除项目

tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

moc.deleteObject()之后

或添加NSFetchedResultsControllerDelegate方法

func controllerWillChangeContent(controller: NSFetchedResultsController) {
  self.tableView.beginUpdates()
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
  switch type {
  case .Insert:
    tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
  case .Delete:
    tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
  case .Update:
    self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, atIndexPath: indexPath!)
  case .Move:
    tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
    tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
  }
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
  self.tableView.endUpdates()
  self.tableView.reloadData()
}

关于ios - Swift 2 核心数据 : delete object ok! 但 TableView 中没有刷新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33653488/

相关文章:

ios - 函数在闭包完成之前返回 Firebase

ios - UIViewController 的循环展示

ios - 通过导航 Controller 返回上一个 View 时调用哪个方法?

ios - TableView NSInternalInconsistencyException

ios - 如何在 Realm Swift 中重新排序 UITableView 单元格?

ios - 核心数据注释——修复缺失的删除传播

ios - 如何在 iOS Swift 3 中允许 CollectionViewCell 文本字段的所有字符均为大写字母且字符限制为 1?

ios - 如何在从 UITableViewController 到 UIViewController 的 Unwind segue 中声明 UITableViewController

iphone - 拥有多个NSPersistentStoreCoordinators是否有意义?

ios - 为什么我的核心数据迁移因 EXC_BAD_ACCESS 而崩溃