Alex Rivera | Logout

UITableView cell textLabel color

Asked 2012-03-16T07:08:20.853
16

I have a simple issue with UITableViewCell. What I want is to change the text color of a selected cell. In my cellForRowAtIndexPath method, I set:

cell.textLabel.textColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.highlightedTextColor = [UIColor orangeColor];

If the selectionStyle is UITableViewCellSelectionStyleNone, highlightedTextColor will not change. So I use these two methods to set the text color:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor orangeColor];    
  return indexPath;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor];    
  return indexPath;
}

It works, but when scrolling the tableview, the color changes back.

Edit
Report

1 Answer

14

All that you have to do is

cell.textLabel.textColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.highlightedTextColor = [UIColor orangeColor];

inside if(cell == nil)

that will work fine.

answered 2012-03-16T07:15:31.200

Your Answer