由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'无效更新:第0部分中的行数无效。更新后现有部分中包含的行数(1)必须等于更新前的该部分中包含的行数(1),加上或减去从该部分插入或删除的行数(0插入,1删除)和加或减移入的行数或超出该部分(0移入,0移出)。'
以下是代码:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = delegate.managedObjectContext!
var error: NSError?
let fetchRequest = NSFetchRequest(entityName: "Task")
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as! [NSManagedObject]
managedContext.deleteObject(fetchedResults[indexPath.row])
if managedContext.save(&error) == true {
println("Yes, you did it!")
}
//All the above code works fine.
table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
}
更新:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
答案 0 :(得分:2)
您必须在调用tasks
之前更新deleteRowsAtIndexPaths
变量:
managedContext.deleteObject(fetchedResults[indexPath.row])
tasks.removeAtIndex(indexPath.row)
table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
答案 1 :(得分:1)
core data
删除了数据,但该数据仍在源数组中。