自定义侧滑删除cell按钮样式

一般的情况下我们都会使用系统自带的侧滑删除,但是有局限性,只有文字没有图片.如果碰上产品或者ui没得商量的脾气.只好忍气吞声硬着头皮来了.
想法是在cell中添加一个可以滑动的控件
目前来看能滑动的控件Tableview ScrollView Collectionview.就用scrollview来作为滑动控件
设置scrollview的滚动范围就是 当前cell的宽度加上删除按钮的宽度
我们只需要将将要显示的数据添加到scrollview上即可
cell.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
@protocol DeleteTableViewCellDelegate <NSObject>

- (void)cellScrollDelete:(UITableViewCell *)cell;

@end
@interface DeleteTableViewCell : UITableViewCell
@property UILabel *nameLabel;
@property id<DeleteTableViewCellDelegate> delegate;
@property UIScrollView *mainScrollView;
@property UIButton *deleteBtn;
@end

NS_ASSUME_NONNULL_END

cell.m

#import "DeleteTableViewCell.h"
@interface DeleteTableViewCell()<UIScrollViewDelegate>

@property UIView *bgView;

@end
@implementation DeleteTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.layer.masksToBounds = YES;
        self.mainScrollView = [[UIScrollView alloc] init];
        self.mainScrollView.showsHorizontalScrollIndicator = NO;
        self.mainScrollView.delegate = self;
        self.mainScrollView.userInteractionEnabled = YES;
        [self.contentView addSubview:self.mainScrollView];
        // 这就可以随意更改删除样式了
        self.deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
        [self.deleteBtn setBackgroundColor:[UIColor redColor]];
        [self.mainScrollView addSubview:self.deleteBtn];
        
        self.bgView = [[UIView alloc] init];
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.bgView.layer.cornerRadius = 10;
        self.bgView.layer.masksToBounds = YES;
        [self.mainScrollView addSubview:self.bgView];
        
        
        self.nameLabel = [[UILabel alloc] init];
        self.nameLabel.font = [UIFont systemFontOfSize:19];
        [self.bgView addSubview:self.nameLabel];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    // 设置滑动视图的偏移量是:屏幕宽+删除按钮宽
    self.mainScrollView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
    self.mainScrollView.contentSize = CGSizeMake(96 + self.contentView.frame.size.width, 0);
    self.deleteBtn.frame = CGRectMake(self.contentView.frame.size.width+16, 0, 80, self.contentView.frame.size.height);
    self.bgView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
    self.nameLabel.frame = CGRectMake(16, 16, self.bgView.frame.size.width-32-50, 30);
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (self.delegate) {
        [self.delegate cellScrollDelete:self];
    }
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint movePoint = self.mainScrollView.contentOffset;
    //    NSLog(@"%f",movePoint.x);
    if (movePoint.x < 0) {
        [self.mainScrollView setContentOffset:CGPointMake(0, 0)];
    }
    // 拖拽时 改变self.deletebutton 的大小
    // 防止我们使劲往左侧滑动时deletebtn会紧贴着self.bgview 
    if (movePoint.x > 96) {
        self.deleteBtn.frame = CGRectMake(self.contentView.frame.size.width+movePoint.x-80, 0, 80, self.contentView.frame.size.height);
    }
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    CGPoint endPoint = self.mainScrollView.contentOffset;
    if (endPoint.x < 96) {
        [self.mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}

调用当前自定义侧滑删除cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DeleteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"applyPurchasePlanReuse"];
    if (!cell) {
        cell = [[DeleteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"applyPurchasePlanReuse"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.nameLabel.text = [NSString stringWithFormat:@"%ld.数据",indexPath.row + 1];
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
// 这里是tableview刚开始滚动时将上一次操作的cell侧滑删除返回到正常状态
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    DeleteTableViewCell *tempCell = [self.tempTableview cellForRowAtIndexPath:self.openindexPath];
    [tempCell.mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
// 实现的协议方法
- (void)cellScrollDelete:(UITableViewCell *)cell{
 // 1.查找当前的cell的indexpath
    NSIndexPath *tempIndexPath = [self.tempTableview indexPathForCell:cell];
 // 2.判断上一次操作的cell是否与本次的一致
   if (tempIndexPath.row != self.openindexPath.row) {
// 不一致时,将上一次的侧滑删除返回到正常状态
        DeleteTableViewCell *tempCell = [self.tempTableview cellForRowAtIndexPath:self.openindexPath];
        [tempCell.mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
// 3.重新赋值
    self.openindexPath = tempIndexPath;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,635评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,628评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,971评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,986评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,006评论 6 394
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,784评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,475评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,364评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,860评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,008评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,152评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,829评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,490评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,035评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,428评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,127评论 2 356

推荐阅读更多精彩内容

  • 《写作法宝——非虚构写作指南》,是一本被喻为非虚构写作“圣经”的著作。又是行动营剽悍一只猫推荐必读的好书,读完后才...
    心智升级之路阅读 500评论 2 0
  • 今天的内容是:养鱼就是要养水,养人就是要养空气。 专注过程,而不是结果,在过程中体验酸甜苦辣,你会很快看到结果的。...
    长期主义者庆福阅读 213评论 0 0
  • 每个妈妈眼里的自家的熊孩子,都是别人眼里的好孩子。 和朋友在一起聊育儿的话题,通常开头是这样的:我家孩子让我头疼死...
    静待春暖花开阅读 535评论 0 1
  • 总是被这样问:“你干什么了?你在家干什么了?”问的我一脸无辜 不知该如何回答。 早上十点起床 属实有一点晚,习惯了...
    薛艳云阅读 270评论 0 0