iOS自定義tableView多選cell選中樣式

前段時間寫了一篇tableView多選刪除的文章http://www.itdecent.cn/p/abfc4d7e56a9,然后一直有人問怎么自定義多選狀態(tài)的按鈕樣式,系統(tǒng)默認(rèn)是藍(lán)色的:就像這樣

系統(tǒng)自帶效果

我們想實(shí)現(xiàn)右邊的藍(lán)色按鈕自定義樣式,我找了個橙色的圖片(當(dāng)然實(shí)現(xiàn)什么效果看你找什么樣的圖片),實(shí)現(xiàn)的效果是這樣的,


實(shí)現(xiàn)自定義的效果

注意:如果只是想改變左邊選中顏色,只要改變cell前景色就可以cell.tintColor = [UIColor orangeColor];

如果要替換選中的圖片在往下看,我的思路是這樣的:首先看這個選中狀態(tài)按鈕的層級關(guān)系


按鈕的層級關(guān)系圖1

按鈕的層級關(guān)系圖2

看到了層級關(guān)系,這個選中狀態(tài)按鈕不是一個button而是一個UIImageView,這個UIImageView在UITableViewCellEditControl上面,這個類沒見過,但是看見了Control的后綴他一定繼承自UIControl,確定了他的層級關(guān)系,第一個想法是在初始化cell的方法中把選中圖片替換掉就可以了

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        for (id subCell in cell.subviews) {
            if ([subCell isKindOfClass:[UIControl class]]) {
                
                for (UIImageView *circleImage in [subCell subviews]) {
                        circleImage.image = [UIImage imageNamed:@"CellButtonSelected"];
                    
                }
            }
        }

但是試了之后發(fā)現(xiàn)不管用,不進(jìn)去最后設(shè)置image的方法,想想這個選中圖片有兩種狀態(tài)選中和未選中狀態(tài),我要設(shè)置的是選中狀態(tài)的圖片,而cell初始化沒有imageView,所以在在自定cell布局中寫,或者在didSelectRowAtIndexPath的方法中寫,先說在didSelectRowAtIndexPath的方法

 NSArray *subviews = [[tableView cellForRowAtIndexPath:indexPath] subviews];
    for (id subCell in subviews) {
        if ([subCell isKindOfClass:[UIControl class]]) {
            
            for (UIImageView *circleImage in [subCell subviews]) {
                circleImage.image = [UIImage imageNamed:@"CellButtonSelected"];
            }
        }
        
    }

點(diǎn)擊cell,管用!這種方式可以達(dá)到不想要cell的選中樣式的效果,平時你設(shè)置cell.selectionStyle = UITableViewCellSelectionStyleNone;這樣cell左邊的選中圓圈也不見了,但是如果在didDeselectRowAtIndexPath中設(shè)置未選中圖片圓圈,在didSelectRowAtIndexPath中設(shè)置選中圖片,左邊選中圓圈就顯示了。

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSArray *subviews = cell.subviews;
    for (id subCell in subviews) {
        if ([subCell isKindOfClass:[UIControl class]]) {
            
            for (UIImageView *circleImage in [subCell subviews]) {

                circleImage.image = [UIImage imageNamed:@"CellButton"];

            }
        }
        
    }

效果是這樣的,沒有了cell的選中樣式

沒有了cell的淺藍(lán)色選中樣式

但是有一個問題就是全選的時候還是系統(tǒng)自帶的藍(lán)色圓圈,因?yàn)槿x的方法是

for (int i = 0; i < self.dataArr.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
}

沒有調(diào)用didSelectRowAtIndexPath方法,所以在全選點(diǎn)擊后要替換每個藍(lán)色圓圈

for (int i = 0; i < self.dataArr.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];
            NSArray *subviews = [[self.tableView cellForRowAtIndexPath:indexPath] subviews];
            for (id subCell in subviews) {
                if ([subCell isKindOfClass:[UIControl class]]) {
                    
                    for (UIImageView *circleImage in [subCell subviews]) {
                        circleImage.image = [UIImage imageNamed:@"CellButtonSelected"];
                    }
                }
                
            }   
        }

還有一種簡單的方法在自定義的cell里面布局里面設(shè)置,就不用再在全選點(diǎn)擊事件中加入上面的代碼了:

-(void)layoutSubviews
{
   
    for (UIControl *control in self.subviews){
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
            for (UIView *view in control.subviews)
            {
                if ([view isKindOfClass: [UIImageView class]]) {
                    UIImageView *image=(UIImageView *)view;
                    if (self.selected) {
                        image.image=[UIImage imageNamed:@"CellButtonSelected"];
                    }
                    else
                    {
                        image.image=[UIImage imageNamed:@"CellButton"];
                    }
                }
            }
        }
    }
    
    [super layoutSubviews];
}

但是這兩種方法都有一個問題,就是當(dāng)你長按一個按鈕時看到的還是系統(tǒng)的藍(lán)色圓圈,還要改變長按編輯的代碼

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
        for (UIControl *control in self.subviews){
            if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
                for (UIView *view in control.subviews)
                {
                    if ([view isKindOfClass: [UIImageView class]]) {
                        UIImageView *image=(UIImageView *)view;
                        if (!self.selected) {
                            image.image=[UIImage imageNamed:@"CellButton"];
                        }
                    }
                }
            }
        }
    
}

GitHub地址:https://github.com/D-james/MultipleSelectCell
希望可以幫到你

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容