【问题标题】:iOS: What is the default background color of UITableViewCelliOS:UITableViewCell 的默认背景颜色是什么
【发布时间】:2011-12-24 11:19:03
【问题描述】:

我想将 tableViewCell 内标签的背景颜色设置为不透明,但我需要 Apple 提供的 uiTableViewCell 的默认背景颜色。

那么我怎样才能得到这个背景或者它是什么颜色的呢?

问候

【问题讨论】:

  • 不改变背景颜色时,它们有什么颜色?这应该是默认值。
  • 如何使用这些标签的 superView 属性并从该点获取背景颜色?
  • 这也行不通。我得到黑色背景。这可能是因为单元格的 backgroundColor 为 null...

标签: ios uitableview default background-color


【解决方案1】:

要重置回收(出队)UITableViewCell 的颜色,这在 iOS 5、6 和 7 中对我有用:

cell.backgroundColor = nil;

正如其他人在这里所建议的那样,我为新创建的单元格记录了该属性的值。我看到了null。这让我产生了取消该财产的想法。似乎适用于所有 3 个 iOS 版本。

我隐约记得,iOS 已经彻底改变了UITableView 和 UITableViewCell 中颜色的默认设置方式。在某些版本中,父容器颜色被拾取。在某些版本中,默认颜色是灰白色,但会随着时间而改变。在 iOS 7 中,无论父母如何,都会应用白色。注意:不要在这一段引用我的话;如果您在乎,请查找。关键是告诉你设置特定颜色的答案会给你不好的建议。

【讨论】:

    【解决方案2】:

    您可以记录单元格的背景颜色

    NSLog(@"cell background color is %@", cell.backgroundColor);
    

    或单元格的内容视图

    NSLog(@"cell contentView's background color is %@", cell.contentView.backgroundColor);
    

    您也可以直接设置标签的颜色:

    label.backgroundColor = cell.contentView.backgroundColor;
    

    【讨论】:

    • 都不行!?我得到这个:“单元格背景颜色是(null)”和“单元格 contentView 的背景颜色是(null)”。我在设备和模拟器上的 cellForRowAtIndexPath 方法中记录了这个。这很奇怪。这是为什么呢?
    • U,我有没有提到我有自定义单元格“cell = [[TableViewCellController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];”。这是个问题吗?
    • TableViewCellController?是什么的子类?
    • 它是 UITableViewCell 的子类,就像它应该是的那样......我在 uiTableView 类及其 uiTableViewCell 子类中的 backgroundColor 都为 null。这真的很奇怪。你知道为什么会这样吗?
    【解决方案3】:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        UIColor *color = [cell backgroundColor];
        NSLog(@"%@",color);
    

    结果是

    2011-11-08 17:09:20.101 test1[5439:207] UIDeviceRGBColorSpace 1 1 1 1

    表示默认颜色为 Red=1, Green=1, Blue=1, alpha=1。它是白色的。

    注意,UITableViewCell 也有contentView 属性,它包含真正的可见颜色。

    【讨论】:

      【解决方案4】:

      我找到了解决办法:Different cell bg color depending on iOS version (4.0 to 5.0)

      总结一下我只是实现方法

      - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
      

      谢谢!

      【讨论】:

        猜你喜欢
        • 2011-06-07
        • 1970-01-01
        • 2013-11-09
        • 1970-01-01
        • 2023-01-22
        • 2011-10-09
        • 2014-04-05
        • 2012-02-03
        • 1970-01-01
        相关资源
        最近更新 更多