簡體   English   中英

如何刪除表視圖中的單元格

[英]how to delete the cell in table view

有人可以建議我的代碼中存在什么問題.....

self.modleClass.foldersAray有一個對象,tableview有一個section和一行

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

這樣做,

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

      if (editingStyle == UITableViewCellEditingStyleDelete) {

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

          [tableView reloadData];
      }
}

它夠了.........

如果tableView基於foldersAray ,則從該數組中刪除對象並重新加載tableview將導致刪除此單元格。 您可以刪除該行:

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

此錯誤意味着您必須更新數據源(包含數據,字典或您使用的任何內容的數組)

例如:刪除前有5個單元格,dataArray中有5個對象。 在調用deleteRowsAtIndexPaths:之前deleteRowsAtIndexPaths:我們必須從dataArray中刪除與該單元格關聯的對象,然后才調用deleteRowsAtIndexPaths: . 而是使用[tableView reloadData] ,使用

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

PS我看,你從數據源中刪除對象,所以仔細檢查,也許你錯過了什么

如果你想要動畫試試這個,如果你不想動畫使用@Mountain Lion的答案。

 -(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 animated:YES];

在你的方法里面開始編輯。

如果你需要的內存對你來說無關緊要......那么你可以通過將高度設置為0來顯着刪除(隱藏)單元格。

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

PS:上面的虛擬代碼將大大刪除備用單元格。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM