简体   繁体   中英

How can I change a UITableViewCell text color in it's highlighted state?

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
    if (selected) {
        companyLabel.textColor = [UIColor whiteColor];
        priceLabel.textColor = [UIColor whiteColor];
        changeLabel.textColor = [UIColor whiteColor];
        symbolLabel.textColor = [UIColor whiteColor];
    }
    else
    {
        companyLabel.textColor = [UIColor blackColor];
        priceLabel.textColor = [UIColor blackColor];
        symbolLabel.textColor = [UIColor blackColor];

        if([changeLabel.text doubleValue] < 0)
        {
           changeLabel.textColor = [UIColor colorWithRed:239.0/255.0 green:16.0/255.0 blue:52.0/255.0 alpha:1.0];
        }
        else if([changeLabel.text doubleValue] > 0)
        {
           changeLabel.textColor = [UIColor colorWithRed:77.0/255.0 green:161.0/255.0 blue:0.0 alpha:1.0];
        }
    }

}

My text doesn't turn white until AFTER the next view is in the process of being pushed onto the navigation stack.

I want it to turn white even as a user tap+holds a cell.

UILabel s have a highlightedTextColor property. When a view like a UITableViewCell goes into its highlighted state, all subviews, including your label, should automatically be changed to use their highlighted properties. If it's still not working there is a field for disabling that feature too that you would want to check on.

You don't want to do it in any tableview delegate methods. You have to set highlighted text color to the UILabel as given

[myLabel setHighlightedTextColor:[UIColor whiteColor]];

This will work. You dont want to handle even unhighlighted state too.

Cheers !

I'd use the UITableViewDelegate methods to achieve this. UILabel's can have set colours, so why not do something use didSelectRowAtIndex and didDeselectRowAtIndex. In the didSelectRowAtIndex, set the label to your desired colour and then in didDeselectRowAtIndex set the label back to black.

You could replace the label with a UIButton and act on the touchDown event?

Button's still have the titleLabel property so it can display text fine, and you'll be able to act on touches easier. I can't see much of your code though, so I don't know what you're doing with these labels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM