此代码正常工作,在Core Data中删除。表视图集右侧为红色,但不删除表视图中的行。
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
}
}
如果我确实停止了应用程序然后再次运行它,则表视图不会对已删除的行进行采样并对它们保留的行进行采样。
答案 0 :(得分:2)
通过添加
显式删除该项目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()
}