删除索引路径错误处的行
Delete row at index path error
由于未捕获的异常 'NSInternalInconsistencyException',正在终止应用程序,原因:'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
代码如下:
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
}
- 问题是您从
core data
中删除了数据,但该数据仍在您的源数组中。
- 您必须在删除行之前从源数组中删除该数据
您必须在调用 deleteRowsAtIndexPaths
之前更新 tasks
变量:
managedContext.deleteObject(fetchedResults[indexPath.row])
tasks.removeAtIndex(indexPath.row)
table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
由于未捕获的异常 'NSInternalInconsistencyException',正在终止应用程序,原因:'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
代码如下:
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
}
- 问题是您从
core data
中删除了数据,但该数据仍在您的源数组中。 - 您必须在删除行之前从源数组中删除该数据
您必须在调用 deleteRowsAtIndexPaths
之前更新 tasks
变量:
managedContext.deleteObject(fetchedResults[indexPath.row])
tasks.removeAtIndex(indexPath.row)
table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)