Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UILabel textColor when UITableViewCell is highlighted?

I have a custom UITableViewCell, with an UILabel and an UIImageView. I want to change the background color and the text color when the cell is highlighted. In my CustomCell's setHighlighted method, I have the following piece of code:

-(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 textColor change in the tableView's didSelectRowAtIndexPath, but it's not the effect that I want - I want the text color to change when the user touches down on the cell - not at touch up. Any suggestions?

like image 412
nightfixed Avatar asked Dec 15 '22 14:12

nightfixed


1 Answers

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

cell.textLabel.highlightedTextColor = [UIColor blueColor];
like image 132
Simon Degn Avatar answered Feb 15 '23 23:02

Simon Degn