iOS - 从 UITableView 崩溃中删除行

Nik*_*nov 2 uitableview ios swift

我正在尝试通过以下方式从表视图和核心数据中删除行:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
        managedObjectContext?.deleteObject(self.fetchedResultsController.objectAtIndexPath(indexPath) as! NSManagedObject)
        managedObjectContext?.save(nil)
        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
        self.tableView.reloadData()
    }
}
Run Code Online (Sandbox Code Playgroud)

但是在self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)我的应用程序上崩溃并出现以下错误:

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 (7) must be equal to the number of rows contained in that section before the update (7), 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).'
Run Code Online (Sandbox Code Playgroud)

也许有人有这样的事情。谁能帮我?

Rei*_*ica 5

删除一行后,将numberOfRowsForSection再次调用您的函数。因此,当您删除该行时,您还应该更新您在上述函数中返回的值。