UITableView左滑设置更多的按钮

本文介绍如何在 iOS 开发中使用 UITableView 的编辑方法 tableView(_:editActionsForRowAtIndexPath:) 来自定义行操作,如删除和置顶功能。通过示例代码展示了如何创建自定义按钮并实现相应的逻辑。

/**

 *  代理方法

 *  它可以设置更多的左侧按钮

 *  但是它要覆盖了之前系统的删除按钮

 */

 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{  //重置删除按钮

    UITableViewRowAction *toTop = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

       // 更新数据

        [self.students removeObjectAtIndex:indexPath.row];

        //更新 tableview

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

        //反馈执行了删除操作

        NSLog(@"删除");

       [tableView setEditing:NO animated:YES];   

    }];

    //设置按钮的背景色

    toTop.backgroundColor =[UIColor redColor];

        //添加一个置顶按钮

    UITableViewRowAction *toTop1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        //刷新数据

        [self.students exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];

        //把所选项置顶

        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];

        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];

        //反馈执行了删除操作

        NSLog(@"置顶");

        [tableView setEditing:NO animated:YES];

    }];

    //设置按钮的背景色

    toTop1.backgroundColor =[UIColor grayColor];

 

      //返回我们所设置的按钮,但是得以数组的形式返回;

    return @[toTop,toTop1];

}

 

转载于:https://www.cnblogs.com/liumu/p/5293669.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值