Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a UIlabel on a UITableViewCell, which I've created programmatically (i.e. not a nib or a subclass).

When the cell is highlighted (goes blue) it makes all the background colors of the UILabels turn clear. I have 2 UILabels where I don't want this to be the case. Currently I'm using UIImageViews behind the UILabel's to make it look like the background color doesn't change. But this seems an inefficient way to do it.

How can i stop certain UILabel's background color changing when the UITableViewCell is highlighted?

question from:https://stackoverflow.com/questions/2965085/uitableviewcell-makes-labels-background-clear-when-highlighted

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
262 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to subclass UITableViewCell and override the following two methods:

Objective-C:

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

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

Swift

override func setSelected(_ selected: Bool, animated: Bool) {
    let color = myLabel.backgroundColor
    super.setSelected(selected, animated: animated)
    myLabel.backgroundColor = color
}

override func setHighlighted(_ highlighted: Bool, animated: Bool) {
    let color = myLabel.backgroundColor
    super.setHighlighted(highlighted, animated: animated)
    myLabel.backgroundColor = color
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...