iOS UITableViewCell 多選時改變編輯狀態(tài)圖片

UITableViewCell 編輯狀態(tài)時 會出現(xiàn)多選按鈕,最近項目有需求這里要改成自己的圖片和去掉一下點擊效果,總結一下:

最終結果

最終結果.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

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末簿晓,一起剝皮案震驚了整個濱河市离陶,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌超歌,老刑警劉巖杉女,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件壹哺,死亡現(xiàn)場離奇詭異,居然都是意外死亡窜锯,警方通過查閱死者的電腦和手機张肾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來锚扎,“玉大人吞瞪,你說我怎么就攤上這事〖菘祝” “怎么了芍秆?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長翠勉。 經(jīng)常有香客問我妖啥,道長,這世上最難降的妖魔是什么对碌? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任荆虱,我火速辦了婚禮,結果婚禮上朽们,老公的妹妹穿的比我還像新娘怀读。我一直安慰自己,他們只是感情好骑脱,可當我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布菜枷。 她就那樣靜靜地躺著,像睡著了一般惜姐。 火紅的嫁衣襯著肌膚如雪犁跪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天歹袁,我揣著相機與錄音坷衍,去河邊找鬼。 笑死条舔,一個胖子當著我的面吹牛枫耳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播孟抗,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼迁杨,長吁一口氣:“原來是場噩夢啊……” “哼钻心!你這毒婦竟也來了?” 一聲冷哼從身側響起铅协,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤捷沸,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后狐史,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體痒给,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年骏全,在試婚紗的時候發(fā)現(xiàn)自己被綠了苍柏。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡姜贡,死狀恐怖试吁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情楼咳,我是刑警寧澤熄捍,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站爬橡,受9級特大地震影響治唤,放射性物質(zhì)發(fā)生泄漏棒动。R本人自食惡果不足惜糙申,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望船惨。 院中可真熱鬧柜裸,春花似錦、人聲如沸粱锐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽怜浅。三九已至铐然,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間恶座,已是汗流浹背搀暑。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留跨琳,地道東北人自点。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像脉让,于是被迫代替她去往敵國和親桂敛。 傳聞我的和親對象是個殘疾皇子功炮,可洞房花燭夜當晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,103評論 1 32
  • 1,Search Bar 怎樣去掉背景的顏色(storyboard里只能設置background顏色,可是發(fā)現(xiàn)cl...
    以德扶人閱讀 2,355評論 2 50
  • 第一重天:肯定優(yōu)點术唬,接納缺點1. 某某人 薪伏,你覺得自己身上有什么優(yōu)點或亮點,是挺不錯的粗仓,是自己或別人挺欣賞的毅该? 我...
    李英花閱讀 230評論 0 0
  • 又是一年春節(jié)來臨眶掌,仿佛今年多了一絲過年的味道,也多了一絲清新的氣息巴碗。一直以來朴爬,在我的心頭緊鎖著一個不解的情結,那就...
    獨行的蒼狼閱讀 434評論 1 4
  • 過往的記不清橡淆,亦不必記召噩。 那便從今日講起。 凌晨之前確乎是哭過的逸爵,而后又因為蝸居具滴,不敢放肆,于是师倔,又不像是哭過构韵,而...
    一一慎修閱讀 150評論 0 0