美文网首页
UITableViewCell选中状态下子UILable背景消失

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

作者: 一盏盏灯 | 来源:发表于2017-03-08 15:42 被阅读21次
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;

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

相关文章

网友评论

      本文标题:UITableViewCell选中状态下子UILable背景消失

      本文链接:https://www.haomeiwen.com/subject/nkvbgttx.html