iOS UITableViewCell 多选时改变编辑状态图片

UITableViewCell 编辑状态时 会出现多选按钮,最近项目有需求这里要改成自己的图片和去掉一下点击效果,总结一下:

最终结果

最终结果.png

1.创建一个继承与UITableViewCell的类EditCell

EditCell.h

#import 

NS_ASSUME_NONNULL_BEGIN

@interface EditCell : UITableViewCell
/** 下标数 */
@property (nonatomic, assign) NSInteger index;
@end

NS_ASSUME_NONNULL_END

EditCell.m

#import "EditCell.h"
#import "Masonry.h"

@interface EditCell ()
/** 下标数 */
@property (nonatomic, strong) UILabel *indexLabel;
@end

@implementation EditCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = [UIColor clearColor];
        UIView *backGroundView = [[UIView alloc]init];
        backGroundView.backgroundColor = [UIColor clearColor];
        self.selectedBackgroundView = backGroundView;
        
        // 创建UI
        [self createUI];
    }
    return self;
}

- (void)createUI {
    /** 下标数 */
    self.indexLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.indexLabel];
}

- (void)setIndex:(NSInteger)index {
    _index = index;
    self.indexLabel.text = @(index).stringValue;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    if (!self.editing) {
        return;
    }
    [super setSelected:selected animated:animated];

    // 修改选中按钮的图片
    if (self.isEditing && self.isSelected) {
        self.contentView.backgroundColor = [UIColor clearColor];
        
        //这里自定义了cell 就改变自定义控件的颜色
        self.indexLabel.backgroundColor = [UIColor clearColor];
        
        UIControl *control = self.subviews.lastObject;
        UIImageView *imgView = control.subviews.firstObject;
        imgView.image = [UIImage imageNamed:@"选中"];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.indexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.equalTo(self.contentView);
    }];
}

@end

2.使用

#import "EditVC.h"
#import "EditCell.h"

@interface EditVC ()
/** 数据源 */
@property (nonatomic, strong) NSMutableArray *dataSoure;
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation EditVC

- (NSMutableArray *)dataSoure {
    if (!_dataSoure) {
        self.dataSoure = [NSMutableArray array];
        for (NSInteger index = 0; index < 100; index++) {
            [_dataSoure addObject:@(index).stringValue];
        }
    }
    return _dataSoure;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(editBtnAction)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(finishBtnAction)];

    // 初始化
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.tableView registerClass:[EditCell class] forCellReuseIdentifier:@"cellID"];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSoure.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    cell.index = indexPath.row;
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == (UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert)) {
        [self.dataSoure removeObject:[self.dataSoure objectAtIndex:indexPath.row]];
        //animation后面有好几种删除的方法
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

#pragma mark - mark
- (void)editBtnAction {
    self.tableView.editing = YES;
}

- (void)finishBtnAction {
    NSArray *indexPaths = self.tableView.indexPathsForSelectedRows;
    for (NSIndexPath *indexPath in indexPaths) {
        NSLog(@"完成: %ld", indexPath.row);
        [self.dataSoure removeObject:[self.dataSoure objectAtIndex:indexPath.row]];
        //animation后面有好几种删除的方法
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
    
    
    self.tableView.editing = NO;
}

@end

你可能感兴趣的:(iOS UITableViewCell 多选时改变编辑状态图片)