UITableViewCell 編輯狀態(tài)時 會出現(xiàn)多選按鈕,最近項目有需求這里要改成自己的圖片和去掉一下點擊效果,總結一下:
最終結果
最終結果.png
最終結果.png
1.創(chuàng)建一個繼承與UITableViewCell的類EditCell
EditCell.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface EditCell : UITableViewCell
/** 下標數(shù) */
@property (nonatomic, assign) NSInteger index;
@end
NS_ASSUME_NONNULL_END
EditCell.m
#import "EditCell.h"
#import "Masonry.h"
@interface EditCell ()
/** 下標數(shù) */
@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;
// 創(chuàng)建UI
[self createUI];
}
return self;
}
- (void)createUI {
/** 下標數(shù) */
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>
/** 數(shù)據(jù)源 */
@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