zoukankan      html  css  js  c++  java
  • 快速实现可自定义的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 亲测无误,轻松愉快!


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

  • 相关阅读:
    【转】一个URL编码和解码的C++类
    ofstream的问题
    如何解决"应用程序无法启动,因为应用程序的并行配置不正确"问题(转载)
    最小生成树 prim & kruskal
    【2018百度之星资格赛】F 三原色图
    cout 按进制数出
    【2018百度之星资格赛】 A 问卷调查
    银河英雄传说
    读入读出挂!!
    关押罪犯
  • 原文地址:https://www.cnblogs.com/duyuiOS/p/5844903.html
Copyright © 2011-2022 走看看