Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView cell textLabel color

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.

like image 552
Guru Avatar asked Mar 16 '12 07:03

Guru


2 Answers

Got the solution by setting color in if (cell == nil) check

if (cell == nil) 
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    cell.textLabel.textColor = [UIColor whiteColor];  
}     

and

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor orangeColor];

}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
        [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor];

}

Thanks

like image 150
Guru Avatar answered Nov 06 '22 06:11

Guru


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.

like image 14
Deepukjayan Avatar answered Nov 06 '22 06:11

Deepukjayan