此代码工作正常,在核心数据中删除。 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/