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;