Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot change UITableView blue highlight color

I set:

cell.selectionStyle = UITableViewCellSelectionStyleGray;

and use the code to highlight a row:

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

The highlight color always blue even I set to gray. If I set:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

it works fine and no highlight. But just not work with:

cell.selectionStyle = UITableViewCellSelectionStyleGray;

It just show blue color instead of gray color. Any idea? Thanks.

like image 638
user2543991 Avatar asked Aug 02 '13 14:08

user2543991


3 Answers

Note that in iOS 7, using

[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

will not work as expected, because in iOS 7 this is now gray, even if you pass the constant above. See:

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

like image 150
Vern Jensen Avatar answered Nov 06 '22 17:11

Vern Jensen


Just add this in your method its 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 question feel free to ask

like image 29
Waseem Shah Avatar answered Nov 06 '22 17:11

Waseem Shah


Implement it as follows:-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {

     [cell setSelectionStyle:UITableViewCellSelectionStyleGray];       
}

OR

Set the selectedBackgroundView's color as what you want in your custom tableview cell (which 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 -tableView:cellForRowAtIndexPath: method:

//...
[cell setSelectedBackgroundView:selectedBackgroundView];
//...
like image 30
icodebuster Avatar answered Nov 06 '22 17:11

icodebuster