在iOS中,可以使用UITableView来显示数据,并且可以通过侧滑手势来实现删除功能。下面是实现UITableView侧滑删除的步骤:

  1. 遵循UITableViewDelegate协议,并实现tableView:commitEditingStyle:forRowAtIndexPath:方法。这个方法会在用户点击删除按钮时调用。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 执行删除操作
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
  1. 设置tableView的editing属性为YES,以启用编辑模式。
self.tableView.editing = YES;
  1. 在UITableViewCell中添加删除按钮。可以通过实现tableView:editActionsForRowAtIndexPath:方法来定制删除按钮的标题和样式。
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        // 执行删除操作
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }];
    return @[deleteAction];
}

以上就是实现UITableView侧滑删除的步骤。当用户侧滑某一行时,会显示出删除按钮,点击删除按钮后会触发删除操作,同时更新UITableView的显示

ios object-c uitableview 侧滑删除

原文地址: https://www.cveoy.top/t/topic/iDHu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录