iOS - 从UITableView崩溃中删除行

时间:2015-04-05 15:50:43

标签: ios uitableview 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()
    }
}

但在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).'

也许某人有这样的事情。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

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