【问题标题】:cannot set background color of UITableViewCell in iOS6?iOS6 无法设置 UITableViewCell 的背景颜色?
【发布时间】:2013-12-20 20:04:30
【问题描述】:

我开始在模拟器下测试我的应用程序,因为我没有任何 iOS 6 设备并且偶然发现了奇怪的问题。我无法设置 UITableViewCell 的 backgroundColor 属性。如果我这样做:

cell.contentView.backgroundColor = [UIColor redColor];

当我使用它时,它仅适用于 iOS 6:

cell.backgroundColor = [UIColor redColor];

或者这个

[cell setBackgroundColor:[UIColor redColor]];

它仅适用于 iOS7。

当我同时使用cell.contentViewcell.backgroundColor 时,它同时适用于iOS ......难道不应该是这样一个“简单”属性的答案吗?或者可能是一些模拟器错误?

更新: 如果它改变了同一个表格视图和单元格中的任何内容,我无法通过 StoryBoard 和代码设置 accessoryType...

UPDATE2:出于某种原因,将 tableview 样式设置为完全删除了我的所有更改,但按预期分组显示...

【问题讨论】:

    标签: ios objective-c uitableview ios-simulator


    【解决方案1】:

    在 iOS6 中,您需要在 willDisplayCell 中更改单元格的背景颜色。 这也适用于 iOS7,无需特定版本的代码。

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

    【讨论】:

    • 这在 iOS 7 中对我有用。对于 iOS 6,我需要在 cellForRowAtIndexPathwillDisplayCell 方法中设置 backgroundColor。如果没有(如果仅在 willDisplayCell 中设置),则在添加新行时,willDisplayCell 只会为新显示的单元格激活,但其他仍然可见的单元格背景颜色将被重置。
    • 效果很好,应该被接受为真实答案。
    【解决方案2】:

    我没有看到将 contentView 和直接设置为单元格的背景的问题。 iOS 7 对这个类有很大的改变。如果您需要与旧系统兼容,那么您需要做这类事情。

    所以是的,你应该同时使用:

    cell.contentView.backgroundColor
    cell.backgroundColor
    

    【讨论】:

      【解决方案3】:

      尝试更改单元格的 backgroundView 颜色而不是 backgroundColor。

      UIView *myView = [[UIView alloc] init];
      myView.backgroundColor = [UIColor whiteColor];
      cell.backgroundView = myView;
      

      【讨论】:

        【解决方案4】:

        如果你设置(假设你使用标准单元):

        cell.textLabel.backgroundColor = [UIColor clearColor];
        

        ...那么你只需要使用:

        cell.contentView.backgroundColor = [UIColor redColor];
        

        iOS 6 似乎将表格的背景颜色复制到 textLabel。

        【讨论】:

          【解决方案5】:

          尝试添加此代码:

          cell.backgroundView.backgroundColor = [UIColor clearColor];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-04
            • 2014-03-29
            • 1970-01-01
            • 2012-06-05
            • 1970-01-01
            相关资源
            最近更新 更多