2015-09-26 56 views
1

我有一個tableview,從數組中加載數據稱爲self.data。 然後我試圖刪除行,所以第i個從數據數組中刪除它,然後我試圖從表視圖與移除:從tableview中刪除一行導致崩潰?

Data *d=[[Data alloc] init]; 
    [d removeObject:ID]; 

    NSLog(@"%ld",editingRow); //the right row to remove 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:editingRow inSection:0]; 


    //update table 
    [self.tableView beginUpdates]; 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    [self.tableView endUpdates]; 

崩潰是這樣的:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), 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

嘗試下面的代碼TableViewDelegate

[self.tableView beginUpdates]; 

    [d removeObject:ID]; 

    [self.tableView deleteRowsAtIndexPaths:[NSIndexPath indexPathForRow:ID inSection:0] 
        withRowAnimation:UITableViewRowAnimationRight]; 

    [self.tableView endUpdates]; 
+0

感謝。這和我所做的完全一樣。我沒有看到這個答案如何帶來新的東西。 – Curnelious

+0

您刪除帶ID的對象,但必須根據indexPath.row刪除。 –

+0

錯了。我正在移除一個帶有ID的對象,因爲數據庫順序與tableview順序不同。此外,您刪除的行數並不重要,但db中的行數爲-1。 – Curnelious