无法更改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;
它只显示蓝色而不是灰色。 任何想法? 谢谢。
实施如下: –
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath { [cell setSelectionStyle:UITableViewCellSelectionStyleGray]; }
要么
在您的自定义tableview单元格(它是UITableViewCell的子类)中将selectedBackgroundView
的颜色设置为您想要的颜色:
UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame]; [selectedBackgroundView setBackgroundColor:[UIColor redColor]]; // set color here [self setSelectedBackgroundView:selectedBackgroundView];
或者你可以在-tableView:cellForRowAtIndexPath:
方法中配置它:
//... [cell setSelectedBackgroundView:selectedBackgroundView]; //...
请注意,在iOS 7中,使用
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
将无法按预期工作,因为在iOS 7中,即使您传递上面的常量,它现在也是灰色的。 看到:
只需在您的方法中添加它对我的工作
- (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; }
如果您有任何问题,请随时提出
确保在InterfaceBuilder或cellForRowAtIndexPath:
方法中执行此类配置。
对“UITableViewCellSelectionStyleBlue”进行全局搜索,以确保您没有输入错字。