Core data 在swift中从tableview中删除项目时出错

Core data 在swift中从tableview中删除项目时出错,core-data,swift,ios8,tableview,Core Data,Swift,Ios8,Tableview,我正在尝试在我的应用程序中实现删除功能,允许用户删除存储在核心数据中并通过表视图显示的信息 这是我的密码: func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if (editingStyle == .Delete){ tableView.

我正在尝试在我的应用程序中实现删除功能,允许用户删除存储在核心数据中并通过表视图显示的信息

这是我的密码:

 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == .Delete){


    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) //error on this line

        }
}
错误如下:

Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UITableView.m:1582
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (6), 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).'


我做错了什么?如何修复此错误?

删除核心数据项,并让代理回调负责删除表视图行:

if editingStyle == .Delete {
  let item = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
  self.managedObjectContext.deleteObject(item)
}
行删除发生在委托回调中
controller:didChangeObject:atIndexPath:
forChangeType:newindepath:

if type == .Delete { 
  self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}

当您从tableview中删除行时,您必须从数据源中删除数据,我知道,但由于我的数据源是CoreData,并且数据通过FetchedResultsController呈现,我认为您只需从tableview中删除它,它就会自动从CoreData中删除它……不,您必须从core数据中删除它。委托仅以一种方式从核心数据到表视图。不是从表视图到核心数据OK。。。我试试看。@mstysf您应该添加它作为答案……当然您需要编写代码。否则它不会被编译,也不会被执行;-)。-您认为回调中的“硬编码”是什么?顺便问一下,我应该为委托回调输入什么代码?(对不起,我对swift的iOS开发有些陌生)我把代码写进了我的答案。。。这并不困难:当用户删除项目时,它将从核心数据中删除。调用所提到的委托方法,检查更改的类型,并根据需要删除。