【问题标题】:UITableViewCell background color is different in iOS5 vs iOS4iOS5 和 iOS4 中的 UITableViewCell 背景颜色不同
【发布时间】:2011-12-17 04:41:57
【问题描述】:

在 iOS 5 中,分组的UITableViewCell 使用tableCellGroupedBackgroundColor 颜色而不是常规的whiteColor 颜色。因此,您的自定义单元格的背景与UITableViewCell 的背景不匹配(默认,字幕, Value1、Value2 样式单元格)。

确保在自定义 UITableViewCell 和默认 UITableViewCell(以及相关联的 UILabel 和其他元素)中使用相同背景颜色的最佳方法是什么 - w.r.t. iOS4 和 iOS5 都有吗?

注意:在编辑器中,我可以看到名为 tableCellGroupedBackgroundColor 的新颜色,但没有可用的类别/方法以编程方式获取此颜色。

编辑:

您可以使用以下技术来更改单元格上控件的背景颜色,但在自定义单元格的情况下,您如何根据操作系统(iOS 4 vs iOS 5)设置适当的背景颜色?

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    for (UIView* view in cell.contentView.subviews) {
        view.backgroundColor = cell.backgroundColor;
    }
}

最简单的解决方案(我目前实施的): 这只是确保我的所有单元格都具有白色背景,而与操作系统无关。不知何故,我不需要将白色应用于cell.contentView.subviews

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [cell setBackgroundColor:[UIColor whiteColor]];
}

【问题讨论】:

    标签: ios uitableview colors ios5


    【解决方案1】:

    这是一种快速而简单的方法,假设我们正在处理纯色背景色,没有渐变或类似的东西。

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        for (UIView *subview in [cell.contentView subviews]) {
            subview.backgroundColor = cell.backgroundColor;
        }
    }
    

    在 iOS 5 上,它可以帮助避免难看的视图线。 :)

    【讨论】:

      【解决方案2】:

      或者,您可以将子视图(标签等)的 backgroundColor 设置为 [UIColor clearColor]

      【讨论】:

      • 这不是一个好主意,因为在任何视图上为背景设置透明颜色会降低性能。如果您不需要渐变背景,则应将背景设置为不透明。这是 Apple 推荐的,也是我的经验……
      【解决方案3】:

      如果你继承UITableViewCell,你也可以尝试覆盖setBackgroundColor

      - (void)setBackgroundColor:(UIColor *)backgroundColor {
          [super setBackgroundColor:backgroundColor];
          [[self myLabel] setBackgroundColor:backgroundColor];
      }
      

      iOS 设置单元格的默认背景颜色时似乎使用了此方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-13
        • 1970-01-01
        • 2012-06-05
        • 1970-01-01
        • 2015-07-21
        • 1970-01-01
        • 1970-01-01
        • 2014-04-16
        相关资源
        最近更新 更多