Unable to change UITableView highlight color

I have installed:

cell.selectionStyle = UITableViewCellSelectionStyleGray; 

and use the code to highlight the line:

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection: 0]; [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone 

The backlight color is always blue, even I'm set to gray. If I installed:

 cell.selectionStyle = UITableViewCellSelectionStyleNone; 

It works great and does not stand out. But just don't work with:

 cell.selectionStyle = UITableViewCellSelectionStyleGray; 

It just shows blue instead of gray. Any ideas? Thank.

+9
ios objective-c
Aug 02 '13 at 14:46
source share
5 answers

Implement it as follows: -

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath { [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; } 

OR

Set the selectedBackgroundView color to what you want in your custom table cell (this is a subclass of UITableViewCell):

 UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame]; [selectedBackgroundView setBackgroundColor:[UIColor redColor]]; // set color here [self setSelectedBackgroundView:selectedBackgroundView]; 

or you can configure it in the -tableView:cellForRowAtIndexPath: method -tableView:cellForRowAtIndexPath:

 //... [cell setSelectedBackgroundView:selectedBackgroundView]; //... 
+28
Aug 2 '13 at 2:55
source share

Please note that in iOS 7 using

[cell setSelectionStyle: UITableViewCellSelectionStyleBlue];

will not work as expected, because in iOS 7 it is now gray, even if you pass the constant higher. Cm:

https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/c/tdef/UITableViewCellSelectionStyle

+14
Oct 08 '13 at
source share

Just add this to your method for your work for me

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { .... UIView *bgColorView = [[UIView alloc] init]; bgColorView.backgroundColor = [UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0]; // perfect color suggested by @mohamadHafez bgColorView.layer.masksToBounds = YES; cell.selectedBackgroundView = bgColorView; .... return cell; } 

if you have any questions feel free to ask

+4
Aug 08 '14 at 6:55
source share

Be sure to do this configuration either in the InterfaceBuilder interface or in the cellForRowAtIndexPath: method.

0
Aug 02 '13 at 14:47
source share

Do a global search for "UITableViewCellSelectionStyleBlue" to make sure you don't make a typo.

0
Aug 02 '13 at 15:00
source share



All Articles