2012-08-02 50 views
0

刪除單元格當我嘗試從我UITableView刪除單元格它給了我這個錯誤:錯誤試圖從UITableView中

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046 2012-08-02 16:35:13.941 jsontest2[1551:f803] *** 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 (4) must be equal to the number of rows contained in that section before the update (4), 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).' *** First throw call stack: (0x13e0052 0x1571d0a 0x1388a78 0x9c02db 0xbb518 0xc6211 0xc623f 0x2ce9 0xd289d 0x226d70 0x13e1ec9 0x3a5c2 0x3a55a 0xdfb76 0xe003f 0xdf2fe 0x5fa30 0x5fc56 0x46384 0x39aa9 0x12cafa9 0x13b41c5 0x1319022 0x131790a 0x1316db4 0x1316ccb 0x12c9879 0x12c993e 0x37a9b 0x1c22 0x1b95) terminate called throwing an exception**

這裏是我的代碼:

- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tabela beginUpdates]; 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     // Get the TAG and ID of Bill to delete 
     //UITableViewCell * cell = [self tableView:tabela cellForRowAtIndexPath:indexPath]; 
     //NSLog(@"%d", cell.tag); 

     [tabela deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];  

    } 

    [tabela endUpdates]; 
    [tabela reloadData]; 

} 

感謝幫幫我!

編輯

self.billsArray = [[NSMutableArray alloc] init];
self.billsArray = [resultsDictionary objectForKey:@"bills"];

+0

你需要確保你將其從數據陣列中移除。你如何指定你的行數? – 2012-08-02 19:40:39

+0

用'return [billsArray count];' – 2012-08-02 19:42:57

+1

確保並從billsArray中刪除相應的對象。 – random 2012-08-02 19:52:07

回答

5

在您的billsArray中,確保並刪除相應的被刪除對象。

[billsArray removeObjectAtIndex:indexPath.row]; 

下面是從輸出提示:

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

所以你的UITableView是相當多說:

"Ok, I have 20 rows (which comes from [billsArray count]. Now I'm suppose to delete one row which means I should have 19 rows total....But my delegate is saying I should have 20 rows...Whhhattt dooo I dooo??? .....crash " - UITableView

所以一定要確保從數據數組中刪除的對象,只要你刪除一行。 如果您添加一行,請確保將其添加到您的數據數組中。編輯: (迴應OP編輯)。

這是你的問題。

self.billsArray = [resultsDictionary objectForKey:@"bills"]; 

這創建了一個只讀數組。

你需要做的是這樣的:

self.billsArray = [[NSMutableArray alloc] initWithArray:[resultsDictionary objectForKey:@"bills"]]; 

或者

self.billsArray = [NSMutableArray arrayWithArray:[resultsDictionary objectForKey:@"bills"]]; 

或者你可以做(​​不乾淨的方法):

NSMutableArray *temp = [resultsDictionary objectForKey:@"bills"]; 
self.billsArray = [temp mutableCopy]; 
+0

它回調' **因未捕獲異常'NSInternalInconsistencyException'而終止應用程序,原因:'*** - [JKArray removeObjectAtIndex:]:發送到不可變對象的變異方法'。 – 2012-08-02 20:24:23

+0

你的NSArray需要是NSMutablyArray。 – random 2012-08-02 20:28:04

+0

剛剛在下面看到了你的評論,你如何實例化你的數組?該錯誤意味着您的數組是隻讀的。所以你可能會以錯誤的方式啓動它。 – random 2012-08-02 20:29:56

0

如果您的表只有一個部分,你可以在某個地方的方法調用[billsArray removeObjectAtIndex:[indexPath row]],假設billsArray是NSMutableArray,而不是一個NSArray。如果不是,則可以將其更改爲一個,或將其內容複製到新的NSMutableArray中,然後將該數組用作表數據源。

+0

它的一個'NSMutableArray',現在它返回***終止應用程序,由於未捕獲異常'NSInternalInconsistencyException',原因:'*** - [JKArray removeObjectAtIndex:]:mutating方法發送到不可變對象' – 2012-08-02 20:24:45