简体   繁体   中英

Custom table view cell selection font color

I have a custom UITableViewCell . It has 3 custom labels inside it with custom text.

When i tap on the cell, i want the textColor of all those labels to go white. just like Email app UITableViewCell behavior.

For that, I wrote this in the custom cell class.

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

    // Configure the view for the selected state

    if (self.selected) {
        _subjectLabel.textColor = [UIColor whiteColor];
        _messageLabel.textColor = [UIColor whiteColor];
        _usernameLabel.textColor = [UIColor whiteColor];
    }else {
        _subjectLabel.textColor = [UIColor blackColor];
        _messageLabel.textColor = [UIColor grayColor];
        _usernameLabel.textColor = [UIColor blackColor];
    }



}

I was able to get it. But its not as smooth as it is in the Email app. The color changes only after a small delay. Which method of UITableViewCell should I override to put this code in. I know about the below options, but they don't give the behavior onto custom labels in the custom cell.

typedef enum {
    UITableViewCellSelectionStyleNone,
    UITableViewCellSelectionStyleBlue,
    UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;

Set the label's highlightedTextColor and this will all be done for you automatically. You should not have to do anything special in setSelected at all.

eg

_subjectLabel.highlightedTextColor = [UIColor whiteColor];

当我们选择UITableView任何单元格时, -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath立即调用-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法,可以使用此方法。

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