我怎样才能从UITableView删除选定的行?
可能重复:
将Table更改为Edit模式并删除普通ViewController中的行
我想从tableView中删除选定的行。 我想提供function给用户删除行时,他滑倒或轻弹他的手指在行上。 我知道编辑风格,它提供了一个圆形的红色button与-velogin。
我试过这个代码:
- (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]; }
我得到删除button,但当我点击它SIGABRT错误在那里。
尝试像下面的行:
[bookmarks removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
书签是我加载在表格视图单元格上的数组名称,您应该给您的数组名称代替这个。
删除表格行后,将相应的索引dataSource从dataSource中删除到table.and然后重新加载表格。
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [dataArray removeObjectAtIndex:indexPath.row]; [tableView reloadData];
只需更换代码
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle==UITableViewCellEditingStyleDelete) { [dataArray removeObjectAtIndex:indexPath.row]; } [tableView reloadData]; }
尝试这个
[tableArray removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
必须尝试这个….
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];