63

I am having a tableview and I want to know that how can I change the selected row's text color, say to Red ? I tried by this code :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell= [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];

    cell.text = [localArray objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    cityName = [localArray objectAtIndex:indexPath.row];

    UITableViewCell* theCell = [tableView cellForRowAtIndexPath:indexPath];
    theCell.textColor = [UIColor redColor];
    //theCell.textLabel.textColor = [UIColor redColor];

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

(1) When I select any row then text color changed to the red but when I select another then previously selected row's text remains red. how can I solve this ?

(2) When I scroll the table text color change to the black color how to solve this ?

Thanks..

4 Answers 4

211

Do this in tableView:cellForRowAtIndexPath::

cell.textLabel.highlightedTextColor = [UIColor redColor];

(And don't use cell.text = ... anymore. It has been deprecated for almost 2 years now. Use cell.textLabel.text = ... instead.)


As Raphael Oliveira mentioned in the comments, if the selectionStyle of your cell equals UITableViewCellSelectionStyleNone this won't work. Check Storyboard as well for the selection style.

2
  • the color should be red until I select another row !
    – Maulik
    Apr 30, 2011 at 10:26
  • 7
    Worths mentioning that if the selectionStyle of your cell equals UITableViewCellSelectionStyleNone this won't work. You might not have added this code but check in Storyboard to not waste a lot of time like me trying to figure out why this isn't working. Jul 16, 2013 at 15:05
5

If you want to change the text colour only, with out changing cell background colour. you can use this.Write this code in in cellForRowAtIndexPath method

UIView *selectionColor = [[UIView alloc] init];
selectionColor.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = selectionColor;
cell.textLabel.highlightedTextColor = [UIColor redColor];
1
  • I'm using this method, but on iOS 10 setting the selectedBackgroundView color causes the table separators to disappear on selection. I'm still working on a good solution to this side effect. May 26, 2017 at 18:07
1

I had the same problem, try this!

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    for (id object in cell.superview.subviews) {
        if ([object isKindOfClass:[UITableViewCell class]]) {
            UITableViewCell *cellNotSelected = (UITableViewCell*)object;
            cellNotSelected.textLabel.textColor = [UIColor blackColor];
        }
    }

    cell.textLabel.textColor = [UIColor redColor];

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

That may be the solution to your (and mine) problem.

0

If you're already subclassing UITableViewCell, it's easier/cleaner to set the colors in the awakeFromNib method (assuming you're instantiating from a storyboard or xib).

@implementation MySubclassTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
    self.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.1 green:0.308 blue:0.173 alpha:0.6];
    self.customLabel.highlightedTextColor = [UIColor whiteColor];
}

@end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.