ios 如何从 UITableView 中删除选定的行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14191057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:44:44  来源:igfitidea点击:

How can I delete selected row from UITableView?

iphoneiosuitableviewios4

提问by Dinesh_

Possible Duplicate:
change Table to Edit mode and delete Rows inside a normal ViewController

可能的重复:
将表更改为编辑模式并删除普通 ViewController 中的行

I want to delete selected row from tableView. I want to provide the functionality to the user to delete the row when he slips or flicks his finger on the row. I know the editing style which provides a circular red button with -ve sign on it.

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

I have tried this code:

我试过这个代码:

- (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];
}

I get the delete button but when I click it SIGABRT Error is there.

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

回答by Vishal

Try like below lines:

尝试像下面几行:

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

bookmarks is my array name that I load on table view cell you should give your array name in place of this.

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

回答by Madhu

After removing the table row delete the corresponding index dataSource from your dataSource to table.and then reload table again.

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

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

回答by junaidsidhu

just replace the Code

只需更换代码

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


    [tableView reloadData];
}

回答by ask4asif

Try this

尝试这个

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

回答by user1673099

Must Try This....

一定要试试这个....

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