【问题标题】:Cannot change UITableView blue highlight color无法更改 UITableView 蓝色突出显示颜色
【发布时间】:2013-08-02 14:46:02
【问题描述】:

我设置:

cell.selectionStyle = UITableViewCellSelectionStyleGray;

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

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

即使我设置为灰色,突出显示的颜色也始终为蓝色。如果我设置:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

它工作正常,没有亮点。但只是不适用于:

cell.selectionStyle = UITableViewCellSelectionStyleGray;

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

【问题讨论】:

  • 请发布包含设置选择颜色和执行选择的完整方法主体的代码。
  • 你不会偶然在 iOS 7 中尝试这个,是吗?

标签: ios objective-c


【解决方案1】:

实现如下:-

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

     [cell setSelectionStyle:UITableViewCellSelectionStyleGray];       
}

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

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

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

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

【讨论】:

  • 谢谢。有效。我使用自定义单元格。看起来 setSelectionStyle:UITableViewCellSelectionStyleGray 不适用于自定义单元格。
  • 另外,你不需要设置框架,所以设置选择颜色的最短方法是tableView:cellForRowAtIndexPath:中的这两行:cell.selectedBackgroundView = [[UIView alloc] init]; cell.selectedBackgroundView.backgroundColor = [UIColor redColor];(如果你在init中添加一个autorelease没有使用 ARC。)
【解决方案2】:

请注意,在 iOS 7 中,使用

[单元格设置SelectionStyle:UITableViewCellSelectionStyleBlue];

不会像预期的那样工作,因为在 iOS 7 中它现在是灰色的,即使你传递了上面的常量。见:

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

【讨论】:

    【解决方案3】:

    只需在你的方法中添加它对我的工作

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

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

    【讨论】:

    • 这是一个很好的答案。如果那是您正在寻找的颜色,它会为您提供正确的蓝色。在我的情况下,当我指定默认蓝色 (UITableViewCellSelectionStyleBlue) 时,背景变为灰色。
    • Apple 已弃用蓝色样式,但未使用“弃用”一词。 UITableViewCell.SelectionStyle.blue "蓝色 = 选中时单元格具有默认背景颜色"
    【解决方案4】:

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

    【讨论】:

    • 是的。我第一次在 IB 做,但没有工作。然后,我在 cellForRowAtIndexPath 中做了,仍然没有工作。
    【解决方案5】:

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

    【讨论】:

      猜你喜欢
      • 2011-02-02
      • 2013-04-17
      • 2010-12-12
      • 2013-05-17
      • 1970-01-01
      • 2012-10-24
      • 2013-02-02
      • 2023-03-19
      • 2011-06-19
      相关资源
      最近更新 更多