【问题标题】:How can I delete selected row from UITableView? [duplicate]如何从 UITableView 中删除选定的行? [复制]
【发布时间】:2013-01-07 06:36:06
【问题描述】:

可能重复:
change Table to Edit mode and delete Rows inside a normal ViewController

我想从 tableView 中删除选定的行。我想为用户提供功能,当他在行上滑动或轻弹手指时删除该行。我知道编辑风格,它提供了一个带有 -ve 标志的圆形红色按钮。

我试过这段代码:

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


[tableView beginUpdates];    
if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Do whatever data deletion you need to do...
    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationTop];      
}       
[tableView endUpdates];
[tableView reloadData];
}

我得到了删除按钮,但是当我点击它时,出现 SIGABRT 错误。

【问题讨论】:

标签: iphone ios uitableview ios4


【解决方案1】:

试试下面几行:

[bookmarks removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];

bookmarks 是我在表格视图单元格上加载的数组名称,您应该用数组名称代替它。

【讨论】:

  • 它的工作,但是当我添加另一个数据时,所有删除的数据都会再次显示?
  • in end again select that array again 意思是说重新加载你的数组数据,最后再次选择那个数组...
  • 就我而言,我再次将数组提供给 NSUserDefault 以便重新加载,例如:[[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:@"Bookmarks"];
  • @Vishal 我给出了这个答案,但迟到而且错了,但你解决得很好也很容易.. 继续加油:)
  • 假设我有 cell0 ,cell1 , cell2 我删除 cell1 ,然后在 tableview 中我只显示了 cell0 和 cell1 现在再次如果我删除 cell1 那么它会杀死吗?解释
【解决方案2】:

删除表行后,从你的数据源中删除对应的索引数据源到表中。然后再次重新加载表。

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 [dataArray removeObjectAtIndex:indexPath.row];
  [tableView reloadData];

【讨论】:

    【解决方案3】:

    只需替换代码

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle==UITableViewCellEditingStyleDelete)
        { 
            [dataArray removeObjectAtIndex:indexPath.row];
        }
    
    
        [tableView reloadData];
    }
    

    【讨论】:

      【解决方案4】:

      试试这个

       [tableArray removeObjectAtIndex:indexPath.row];
              [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
      

      【讨论】:

        【解决方案5】:

        一定要试试这个......

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-06-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-05
          相关资源
          最近更新 更多