UITableViewCell選中狀態(tài)下子UILable背景消失問(wèn)題

PS:記錄自己工作學(xué)習(xí)中的一些知識(shí);

在實(shí)際開(kāi)發(fā)中,在一個(gè)自定義的UITableViewCell中,當(dāng)設(shè)置好了UILable的背景顏色后,選中UITableViewCell,會(huì)出現(xiàn)如下情況

00.png

很明顯我們選中的“這是第1行”lable的背景色消失了

解決辦法如下

1.在自定義UITableViewCell中重寫以下2個(gè)方法:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    UIColor *color = self.testLable.backgroundColor;
    [super setSelected:selected animated:animated];
    self.testLable.backgroundColor = color;
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    
    UIColor *color = self.testLable.backgroundColor;
    [super setHighlighted:highlighted animated:animated];
    self.testLable.backgroundColor = color;

}

2.先clearColor,然后設(shè)置layer.backgroundColor(必須clearColor)

cell.testLable.backgroundColor = [UIColor clearColor];
cell.testLable.layer.backgroundColor = [UIColor cyanColor].CGColor;

此時(shí)當(dāng)我們效果為:

01.png

ps:順便說(shuō)個(gè)小技巧:設(shè)置UIlable圓角且不會(huì)觸發(fā)離屏渲染,并且選中背景色不會(huì)消失.

self.testLable.layer.cornerRadius = 5.0f;
self.testLable.backgroundColor = [UIColor clearColor];
self.testLable.layer.backgroundColor = [UIColor cyanColor].CGColor;

當(dāng)然這些方法并不是唯一,謝謝指正

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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