2013-02-22 97 views
1

有一個人所說的其實是在我的代碼的問題小區.....如何刪除表視圖

self.modleClass.foldersAray有一個對象,並實現代碼如下有一個部分和一排

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

{ 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     // Delete the row from the data source 
     [self.tableview setEditing:YES animated:YES]; 

     [self.modleClass.foldersAray removeObjectAtIndex:indexPath.row]; 

     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YEs]; 

     [tableView reloadData]; 
    } 
} 

我收到錯誤如下。

*** 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 (1) must be equal to the number of rows contained in that section before the update (1), 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

此錯誤意味着,你必須更新你的數據源(數組,不管你使用的數據字典或)

例如: 你有5個前刪除和5對象DataArray中。在致電deleteRowsAtIndexPaths:之前,我們必須從dataArray中刪除與該單元關聯的對象,然後才能撥打deleteRowsAtIndexPaths:。也可改爲使用[tableView reloadData],使用

[tableView beginUpdates]; 
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
[tableView endUpdates]; 

P.S.我明白了,您從數據源中刪除的對象,所以仔細檢查它,也許你錯過的東西

2

做這樣的,

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     if (editingStyle == UITableViewCellEditingStyleDelete) { 

      [self.modleClass.foldersAray removeObjectAtIndex:indexPath.row]; 

      [tableView reloadData]; 
     } 
} 

更足夠.........

+0

我這樣做,但細胞沒有刪除...它顯示當我點擊編輯按鈕時的相同狀態... – user1997077 2013-02-22 12:28:24

2

如果您的tableView基於foldersAray,那麼從該數組中刪除一個對象並重新加載tableview將導致刪除該單元格。 你可以刪除行:如果你想動畫試試這個,如果你不想使用動畫獅子@Mountain的回答

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YEs]; 
1

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath { 

     if (editingStyle == UITableViewCellEditingStyleDelete) 
     { 
      [self.modleClass.foldersAray removeObjectAtIndex:indexPath.row]; 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade]; 
     } 
} 

如果你對導航編輯按鈕,你應該叫

[self.tableView setEditing:YES動畫:是];

內部的方法開始編輯。

1

,如果它需要你沒關係的記憶...那麼你就可以極大地設置高度刪除(隱藏)電池爲0

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row%2) { 
     return 0; 
    } 
    return 100; 
} 

P.S:上述虛設碼將極大地刪除備用電池。