iOS版 - 从UITableView中崩溃删除行

问题描述:

我试图从表视图和核心数据删除行方式如下:iOS版 - 从UITableView中崩溃删除行

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

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

+0

从managedObjectContext删除一个对象后。从你用来加载表格视图单元格的数组中删除它。 – Jassi 2015-04-05 16:07:48

+0

不应该是tableView:self.tableView? – Razvan 2015-04-05 16:33:23

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