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

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

最终结果

最终结果.png

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

EditCell.h

#import <UIKit/UIKit.h>

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 ()<UITableViewDataSource, UITableViewDelegate>
/** 数据源 */
@property (nonatomic, strong) NSMutableArray<NSString *> *dataSoure;
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation EditVC

- (NSMutableArray<NSString *> *)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<NSIndexPath *> *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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,145评论 1 32
  • 1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现cl...
    以德扶人阅读 2,494评论 2 50
  • 第一重天:肯定优点,接纳缺点1. 某某人 ,你觉得自己身上有什么优点或亮点,是挺不错的,是自己或别人挺欣赏的? 我...
    李英花阅读 244评论 0 0
  • 又是一年春节来临,仿佛今年多了一丝过年的味道,也多了一丝清新的气息。一直以来,在我的心头紧锁着一个不解的情结,那就...
    独行的苍狼阅读 463评论 1 4
  • 过往的记不清,亦不必记。 那便从今日讲起。 凌晨之前确乎是哭过的,而后又因为蜗居,不敢放肆,于是,又不像是哭过,而...
    一一慎修阅读 157评论 0 0