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

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

#import 

NS_ASSUME_NONNULL_BEGIN
@protocol DeleteTableViewCellDelegate 

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

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

NS_ASSUME_NONNULL_END

cell.m

#import "DeleteTableViewCell.h"
@interface DeleteTableViewCell()

@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;
}

你可能感兴趣的:(自定义侧滑删除cell按钮样式)