iOS tableView右滑显示选择

本文介绍了在iOS开发中如何利用UITableViewRowAction实现tableView的右滑选择功能。通过设置UITableView的代理方法,创建并返回包含多个按钮的数组,每个按钮通过UITableviewRowAction定制样式和行为。在实现过程中,注意自定义按钮会取代系统默认的删除按钮。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何使用UITableViewRowAction实现右滑选择呢?


1、在iOS8以前,我们实现tableview中滑动显示删除,置顶,更多等等的按钮时,都需要自己去实现,在iOS8中系统已经写好了,只要一个代理方法和一个类就行了


2、iOS8的协议对了一个方法,返回值是数组的tableview:editActionForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableviewRowAction


3、在UITableviewRowAction类。我们可以设置按钮的样式,显示文字、背景色和按钮事件(在block内实现)


4、在代理方法中,我们可以常见多个按钮放到数组中返回,最先放入数组的按钮显示在最右边,最后放入的显示在最左边


5、如果自己设定一个或多个按钮,系统自带的删除按钮就消失了

 

设置tableView可以编辑

- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
UITableViewRowAction的使用方法:
+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullable NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;
 
重写UITableViewDelegate的
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
方法。

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(indexPath.row==0){
        // 添加一个删除按钮
        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了删除");
            
        }];
        

    }
    else if (indexPath.row==1){
        // 添加一个删除按钮
        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了删除");
            
        }];
        
        // 添加一个修改按钮
        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了修改");
        }];
        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        

    }
    else if (indexPath.row==2){
        // 添加一个删除按钮
        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了删除");
            
        }];
        
        // 添加一个修改按钮
        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了修改");
        }];
        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        
        // 添加一个发送按钮
        
        sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"发送" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了发送");
        }];
        sanRowAction.backgroundColor=[UIColor orangeColor];

    }
    else{
        // 添加一个删除按钮
        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了删除");
            
        }];
        
        // 添加一个修改按钮
        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了修改");
        }];
        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        
        // 添加一个发送按钮
        
       sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"发送" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了发送");
        }];
        sanRowAction.backgroundColor=[UIColor orangeColor];
        // 添加一个发送按钮
        
        OK = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"OK键" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了OK");
        }];
        OK.backgroundColor=[UIColor purpleColor];
        

    }
    
    
    // 将设置好的按钮放到数组中返回
    if (indexPath.row==0) {
        return @[deleteRowAction];

    }else if (indexPath.row==1){
        return @[deleteRowAction,moreRowAction];

    }else if(indexPath.row==2){
        return @[deleteRowAction,moreRowAction,sanRowAction];

    }else if(indexPath.row==3){
        return @[deleteRowAction,moreRowAction,sanRowAction,OK];
        
    }
    return nil;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值