UITableViewCell选中状态下子UILable背景消失问题

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.png

ps:顺便说个小技巧:设置UIlable圆角且不会触发离屏渲染,并且选中背景色不会消失.

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

当然这些方法并不是唯一,谢谢指正

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容