UITableViewCell Selected Row Text Color Change

I have a table view, and I want to know how to change the selected text color of a string, say, to Red? I tried 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 line, then the text color changes to red, but when I select another, the previously selected line text remains red. how can i solve this?

(2) When I look at the color change of the table text to black, how can I solve this?

Thank..

+58
ios objective-c iphone uitableview
Apr 30 '11 at 10:13
source share
4 answers

Do it in tableView:cellForRowAtIndexPath: ::

 cell.textLabel.highlightedTextColor = [UIColor redColor]; 

(And do not use cell.text = ... anymore, it has been deprecated for almost 2 years. Instead, use cell.textLabel.text = ... )




Like Rafael Oliveira mentioned in the comments, if your cell's selectionStyle is UITableViewCellSelectionStyleNone , this will not work. Check out the storyboard and style choices.

+191
Apr 30 '11 at 10:18
source share

If you want to change only the color of the text, without changing the background color of the cell. you can use this.Write this code in the cellForRowAtIndexPath method

 UIView *selectionColor = [[UIView alloc] init]; selectionColor.backgroundColor = [UIColor clearColor]; cell.selectedBackgroundView = selectionColor; cell.textLabel.highlightedTextColor = [UIColor redColor]; 
+4
Nov 07 '14 at 8:50
source share

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]; } 

This may be the solution to your (and my) problem.

0
Sep 03 '15 at 19:27
source share

If you already subclass UITableViewCell , it is easier / cleaner to set the colors in the awakeFromNib method (assuming you create an instance from the 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 
0
Mar 08 '16 at 20:05
source share



All Articles