Ios 删除索引路径处的行错误
由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:节0中的行数无效。”。更新(1)后现有节中包含的行数必须等于更新(1)前该节中包含的行数,加上或减去从该节中插入或删除的行数(0插入,1删除),加上或减去移入或移出该节的行数(0移入,0移出).' 代码如下:Ios 删除索引路径处的行错误,ios,swift,tableview,Ios,Swift,Tableview,由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:节0中的行数无效。”。更新(1)后现有节中包含的行数必须等于更新(1)前该节中包含的行数,加上或减去从该节中插入或删除的行数(0插入,1删除),加上或减去移入或移出该节的行数(0移入,0移出).' 代码如下: func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCell
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
}
- 问题是您从
核心数据中删除了数据,但该数据仍在源数组中
- 在删除行之前,必须从源数组中删除该数据
删除rowsatindexpaths
之前,必须更新任务
变量:
managedContext.deleteObject(fetchedResults[indexPath.row])
tasks.removeAtIndex(indexPath.row)
table.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
发布您的
numberOfRowsInSection
code似乎在执行deleteRowAtIndexPaths之前,该行已被删除。我与tableView.reloadData交换了deleteRowsAtIndexPaths,它可以工作,但没有动画。看起来在再次调用numberOfRowsInSection
之前,您没有从数据源中删除该项,您必须自己完成。很好,现在可以了。我使用tasks=managedContext.executeFetchRequest(fetchRequest,error:&error)作为![NSManagedObject]若要更新,是否有其他语法可以实现此目的?@BrightFuture您的工具简单高效。@BrightFuture请接受我的回答,您可以单击voteERROR屏幕下方左侧的勾号(复选标记):由于未捕获的异常“NSInternalInconsistencyException”终止应用程序,原因:“无效更新:节0中的行数无效。更新(3)后现有节中包含的行数必须等于更新(3)前该节中包含的行数,加上或减去从该节中插入或删除的行数(0插入,2删除),加上或减去移入或移出该节的行数(0移入,0移出).'