美文网首页iOS开发技巧iOS基础iOS Developer
解决 tableView 选中时 subView 的背景色失效问

解决 tableView 选中时 subView 的背景色失效问

作者: 乒什么乓 | 来源:发表于2016-12-19 22:52 被阅读125次

在一次自定义 cell 时,我设置了一个 subView 的 backgroundColor,并且没有设置 cell 的 selectionStyle = UITableViewCellSelectionStyleNone,然后当点击 cell 的时候,这个 subView 的背景色竟然不生效了,变成了和 cell 的 highlightColor 一样,就像下图一样:

highlightState.jpeg

最终的解决办法就是:在 cell 的状态改变时,重新设置 subView 的背景色:

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

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

上面的 tagView 就是设置了背景色的一个 subView。

如果各位有更好的解决办法,希望共享一下下。

相关文章

网友评论

    本文标题:解决 tableView 选中时 subView 的背景色失效问

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