2013-08-02 68 views
9

我设置:无法更改UITableView的蓝色高亮颜色

cell.selectionStyle = UITableViewCellSelectionStyleGray; 

,并使用代码高亮显示一行:

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

高亮颜色总是很蓝,即使我设置为灰色。如果我设置:

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

它工作正常,没有高光。但只是不适用:

cell.selectionStyle = UITableViewCellSelectionStyleGray; 

它只是显示蓝色而不是灰色。任何想法?谢谢。

+0

请张贴代码,包括完整的方法体您在哪里设置选择颜色并执行选择。 –

+0

你不是偶然在iOS 7中尝试这个,是吗? –

回答

28

如下实现: -

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

    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];  
} 

OR

设置selectedBackgroundView的颜色,只要你想在您的自定义的tableview细胞是什么(是的UITableViewCell的子类):

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

或者您可以在-tableView:cellForRowAtIndexPath:方法中配置它:

//... 
[cell setSelectedBackgroundView:selectedBackgroundView]; 
//... 
+0

谢谢。有效。我使用自定义单元格。看起来像setSelectionStyle:UITableViewCellSelectionStyleGray不适用于自定义单元格。 – user2543991

+6

此外,您不需要设置框架,所以设置选择颜色的最短途径是'tableView:cellForRowAtIndexPath:'中的这两行:cell.selectedBackgroundView = [[UIView alloc] init]; cell.selectedBackgroundView.backgroundColor = [UIColor redColor];' (如果您不使用ARC,请将autorelease添加到init。) –

0

请确保在InterfaceBuilder或cellForRowAtIndexPath:方法中执行此类配置。

+0

是的。我第一次在IB工作,但没有工作。然后,我在cellForRowAtIndexPath中完成了,但仍然无法工作。 – user2543991

0

对“UITableViewCellSelectionStyleBlue”进行全局搜索以确保您没有输入错字。

4

就在你的方法添加这个工作对我来说

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

,如果您有任何问题随时问