PS:记录自己工作学习中的一些知识;
在实际开发中,在一个自定义的UITableViewCell中,当设置好了UILable的背景颜色后,选中UITableViewCell,会出现如下情况
00.png很明显我们选中的“这是第1行”lable的背景色消失了
解决办法如下
1.在自定义UITableViewCell中重写以下2个方法:
- (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,然后设置layer.backgroundColor(必须clearColor)
cell.testLable.backgroundColor = [UIColor clearColor];
cell.testLable.layer.backgroundColor = [UIColor cyanColor].CGColor;
此时当我们效果为:
01.pngps:顺便说个小技巧:设置UIlable圆角且不会触发离屏渲染,并且选中背景色不会消失.
self.testLable.layer.cornerRadius = 5.0f;
self.testLable.backgroundColor = [UIColor clearColor];
self.testLable.layer.backgroundColor = [UIColor cyanColor].CGColor;