无法更改UITableView蓝色高亮颜色

use*_*991 9 objective-c ios

我设置:

cell.selectionStyle = UITableViewCellSelectionStyleGray;
Run Code Online (Sandbox Code Playgroud)

并使用代码突出显示一行:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection: 0];
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone
Run Code Online (Sandbox Code Playgroud)

即使我设置为灰色,高光颜色始终为蓝色.如果我设置:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
Run Code Online (Sandbox Code Playgroud)

它工作正常,没有突出显示.但是不能合作:

cell.selectionStyle = UITableViewCellSelectionStyleGray;
Run Code Online (Sandbox Code Playgroud)

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

ico*_*ter 30

实施如下: -

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

     [cell setSelectionStyle:UITableViewCellSelectionStyleGray];       
}
Run Code Online (Sandbox Code Playgroud)

要么

selectedBackgroundView在自定义tableview单元格(UITableViewCell的子类)中将颜色设置为您想要的颜色:

UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame];
[selectedBackgroundView setBackgroundColor:[UIColor redColor]]; // set color here
[self setSelectedBackgroundView:selectedBackgroundView];
Run Code Online (Sandbox Code Playgroud)

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

//...
[cell setSelectedBackgroundView:selectedBackgroundView];
//...
Run Code Online (Sandbox Code Playgroud)

  • 此外,您不需要设置框架,因此设置选择颜色的最短方法是`tableView:cellForRowAtIndexPath:`:`cell.selectedBackgroundView = [[UIView alloc] init]; cell.selectedBackgroundView.backgroundColor = [UIColor redColor];`(如果你没有使用ARC,请在init中添加自动释放.) (6认同)

Ver*_*sen 15

请注意,在iOS 7中,使用

[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];

将无法按预期工作,因为在iOS 7中,即使您传递上面的常量,它现在也是灰色的.看到:

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


Was*_*hah 5

只需将其添加到您的方法中即可

- (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;
}
Run Code Online (Sandbox Code Playgroud)

如果您有任何疑问,请随时提问