【问题标题】:ios7 UITableViewCell selectionStyle won't go back to blueios7 UITableViewCell selectionStyle 不会变回蓝色
【发布时间】:2013-09-18 14:23:36
【问题描述】:

Xcode 5.0、iOS 7 和更新现有应用。 UITableView 选中的行现在是灰色的,而不是蓝色的。

根据我的阅读,他们将默认 selectionStyle 更改为灰色。但是“蓝色”仍然是 IB 中的一个选项,UITableViewCellSelectionStyleBlue 仍然存在。查看新的 HIG,看起来他们并没有删除蓝色,“设置”应用程序仍在使用蓝色单元格选择。

我尝试在 IB 和代码中设置值,但没有成功。关于我需要做什么才能恢复蓝色选择样式的任何想法?

【问题讨论】:

  • 我现在无法查看,但是在cellForRowAtIndexPath方法中,使用cell.selectionStyle = UITableViewCellSelectionStyleBlue;的语句不起作用?
  • 刚刚测试了它 - 它不起作用。如果我能解决它,我会尽力解决并回复你

标签: uitableview ios7


【解决方案1】:

iOS7 中只有一个 selectionStyle,要更改需要手动进行,如下所示:

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

【讨论】:

  • 我正在考虑尝试这样的事情——唯一的问题是blueColor 是最难看的蓝色阴影。如果有人想在应用程序中实际使用它,则需要使用 RGB 组合以获得更好的颜色
  • 所以我听到的是,默认的蓝色要么不再存在,要么无法正常运行。这几乎也是我的结论,但如果没有未来,他们不会将它从 UI 中删除或弃用它,这似乎非常奇怪。
  • 很好的答案,不过我建议使用[UIColor colorWithRed:(76.0/255.0) green:(161.0/255.0) blue:(255.0/255.0) alpha:1.0] 而不是[UIColor blueColor]
  • 最重要的是,添加 cell.textLabel.highlightedTextColor = [UIColor whiteColor]; 以获取曾经附带的白色文本颜色
  • 如果您想匹配 Apple 的默认色调,请尝试 [UIColor colorWithRed:(0.0/255.0) green:(122.0/255.0) blue:(255.0/255.0) alpha:1.0]
【解决方案2】:

也许它可以帮助你。我有我的自定义单元格并使用所需的颜色选择它,我已经覆盖了 setHighlighted 和 setSelected 现在看起来像这样

#define IOS_7 (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1 ? YES : NO)


    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    [self changeSelectionColorForSelectedORHiglightedState:selected];
    // Configure the view for the selected state
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    [self changeSelectionColorForSelectedORHiglightedState:highlighted];
}

- (void)changeSelectionColorForSelectedORHiglightedState:(BOOL)state
{
    if (IOS_7) {
        if (state) {
            self.contentView.backgroundColor = [UIColor blueColor];
        }
    }
}

【讨论】:

    【解决方案3】:

    我知道这个问题已经得到解答,但我最不想做的就是触摸我所有的 cellForRowAtIndexPath 方法。所以,我在我的 App Delegate 中使用了外观代理。我使用上面@null 的代码来设置选定的背景视图,并在applicationDidFinishLaunching:withOptions: 方法中放置了这段代码。

    UIView *bgColorView = [[UIView alloc] init];
    //the rest of null's code to make the view
    [[UITableViewCell appearance] setSelectedBackgroundView:bgColorView];
    

    然后让白色文字高亮:

    [[UILabel appearanceWhenContainedIn:[UITableViewCell class], nil] setHighlightedTextColor:[UIColor whiteColor]];
    

    这使我的应用发生了全局性变化。 iOS5 中引入了外观代理,Mattt 有一个great article on how to use it at his NSHipster blog

    【讨论】:

    • 下次需要完成此任务时,我一定会记住这一点。
    【解决方案4】:

    我知道我真的迟到了,但我也会提供我的 IOS10 工作。不要碰任何其他代码,而是添加:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor blueColor];
        cell.textLabel.textColor = [UIColor whiteColor];
    
         ... whatever else you do ...
    }
    
    
    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textColor = [UIColor blackColor];
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      相关资源
      最近更新 更多