Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 829
Anand Avatar asked May 19 '12 06:05

Anand


2 Answers

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.

e.g.

_subjectLabel.highlightedTextColor = [UIColor whiteColor];
like image 147
melsam Avatar answered Nov 20 '22 02:11

melsam


When we select any cell of UITableView , -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method called immediately, you can use this.

like image 1
rishi Avatar answered Nov 20 '22 02:11

rishi