2015-04-05 129 views
0

我試圖從表視圖和核心數據刪除行方式如下: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

回答

1

刪除一行後,您的功能numberOfRowsForSection再次被調用。因此,在您刪除該行時,您還應該更新上述函數中返回的值。