快速实现可自定义的tableView滑动删除功能

TableView 是自带滑动删除这个功能,但是有时候并不能满足我们的需求。例如,需求要求删除的那个按钮可自定义,这时候系统的功能就嗝屁了...

当然,现在好用的轮子那么多,相信肯定有能满足你要求的。今天,我来教大家怎么保持系统的滑动删除功能不变,又可以随意的自定义删除按钮,快捷方便!

废话不多说,先上图:


delete.gif
  • 开启TableView 的滑动删除功能
    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
       NSLog(@"BtnClick_%zd",indexPath.row); }
  • 自定义一个 UITableViewCell
    实现如下方法:

    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
      if (self) { UIView *deleteBgView = [UIView new]; deleteBgView.backgroundColor = [UIColor brownColor]; [self.contentView addSubview:deleteBgView]; UIButton *deleteBtn = [UIButton new]; //deleteBtn.backgroundColor = [UIColor yellowColor]; [deleteBtn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal]; [deleteBgView addSubview:deleteBtn]; [deleteBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.offset([UIScreen mainScreen].bounds.size.width); make.top.equalTo(self.contentView); make.bottom.equalTo(self.contentView).offset(1); make.width.equalTo(self.contentView); }]; [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.width.offset(80); make.top.equalTo(deleteBgView); make.bottom.equalTo(deleteBgView); make.left.equalTo(deleteBgView); }]; } return self; }
  • 这里我用了 Masonry 来做布局约束,相信大家一定不陌生,到这里基本完成了,iOS7,8,9 亲测无误,轻松愉快!


注: 这种做法只适用于一个按钮的情况,多个按钮的情况因为无法触发按钮的点击事件,所以无从下手,研究出来的小伙伴,望告知!

转载于:https://www.cnblogs.com/duyuiOS/p/5844903.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值