TableView自定义左划多个按钮

iOS7

前言

苹果给开放的接口只有一个删除按钮,如果你想要像qq那样侧滑有多个按钮,就需要自己写。我用scrollview写了一个。仁者见仁,智者见智,我是用的这个。(考虑到iOS7,8以上都很简单了)。
Demo传送门

核心思想

用scrollview在一个cell上面进行布局,然后进行各种你所需要的操作。进行了简单的动画界面,如果需要自行修补吧,嘎嘎。
这里就不给代理片段了,大家看一下demo应该都会懂。很简单的,希望大家能给个星星哈,谢谢啦。

运行结果

结果

iOS8+

在iOS8之后,这个就好写多了,苹果给了一个新的接口让我们去调用更改按钮的个数:

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

这个方法看着挺吓人的,但是反而很好写:

#pragma mark  core method

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

    // 设置删除按钮
    UITableViewRowAction *noDeleAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"不删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //事件
        NSLog(@"不删除");
    }];

    //收藏
    UITableViewRowAction *noLoveAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"不收藏" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //事件
        NSLog(@"不收藏");
    }];
    //不开心
    UITableViewRowAction *noHappyAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"不开心" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //事件
        NSLog(@"不开心");
    }];

    noDeleAction.backgroundColor = [UIColor blueColor];
    noLoveAction.backgroundColor = [UIColor purpleColor];
    return @[noHappyAction,noLoveAction,noDeleAction];
}

哈哈,所以如果你的项目开始只支持iOS8之上了,那么,你解放了。欢呼吧!!!

Demo传送门

结束

各位大侠,觉得对自己有点用的,有能力的请给个star,谢了~~~~~~~

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值