Change UILabel textColor when UITableViewCell is highlighted?

I have a custom one UITableViewCellwith UILabeland UIImageView. I want to change the background color and text color when the cell is highlighted. In my method CustomCell setHighlighted, I have the following code snippet:

-(void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    if(self) {
        if(highlighted) {
            self.title.textColor = [UIColor whiteColor];
        } else {
            self.title.textColor = [UIColor blackColor];
        }
        //highlight background
        UIView *bgColorView = [[UIView alloc] initWithFrame:self.frame];
        bgColorView.backgroundColor = [UIColor blackColor];
        [self setSelectedBackgroundView:bgColorView];
    }
}

I already tried to put the code for textColorin tableView didSelectRowAtIndexPath, but this is not the effect that I want - I want the text color to change when the user touches the cell - not when touched. Any suggestions?

+4
source share
5 answers

You must use the attribute highlightedTextColor. Set the color for the cell inside tableView:cellForRowAtIndexPathand it should look like this:

cell.textLabel.highlightedTextColor = [UIColor blueColor];
+11
source

try it

[cell.textLabel setHighlightedTextColor:[UIColor yellowColor]]
+1

,

lblPrd.highlightedTextColor = [UIColor whiteColor];
0
source

override method

(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    //TODO set view highlighted color 
}

in custom tableviewcell

0
source

If you have a storyboard or XIB, you can simply set this value to Attributes Inspector > Label > Highlighted.

0
source

All Articles