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 亲测无误,轻松愉快!


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

  • 相关阅读:
    网易云信流媒体服务端架构设计与实现
    从零开始搭建创业公司后台技术栈
    协程(coroutine)简介
    微服务的简介和技术栈
    分布式系统中最容易被忽视的六大“暗流”
    分布式架构的演进
    全网最详尽的负载均衡原理图解
    图解 | 搞定分布式,程序员进阶之路
    Enterprise Library 3.0体验(4):Validation Application Block与ASP.NET的集成
    Enterprise Library 3.0 发布
  • 原文地址:https://www.cnblogs.com/duyuiOS/p/5844903.html
Copyright © 2011-2022 走看看