Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell selected row's text color change

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..

like image 468
Maulik Avatar asked Apr 30 '11 10:04

Maulik


People also ask

How do I change the selection color of Uitableviewcell in iOS?

you can just change the backgroundView's backgroundColor property like so: cell. selectedBackgroundView?. backgroundColor = <your color .

How do I change the background color of a selected cell in Swift?

Swift 3, 4, 5 select cell background colour Next connect your cell's selectedBackgroundView Outlet to this view. You can even connect multiple cells' outlets to this one view. Show activity on this post. For a solution that works (properly) with UIAppearance for iOS 7 (and higher?)


1 Answers

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.

like image 137
Ole Begemann Avatar answered Sep 23 '22 10:09

Ole Begemann