【问题标题】:Cell background and accessory单元格背景和附件
【发布时间】:2011-07-18 16:46:29
【问题描述】:

我正在尝试设置 UITableViewCells 的单元格背景,但我正在为 UIAccessoryView 的背景颜色而苦苦挣扎。它不会改变。

谁能帮我让 UIAccessoryView 的背景颜色透明(或者实际上是任何其他颜色)?

这是我的代码

cell.textLabel.backgroundColor = [UIColor clearColor];
cell.accessoryView.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

if(indexPath.row % 2) {
     cell.contentView.backgroundColor = [UIColor clearColor];
}
else {
    cell.contentView.backgroundColor = [UIColor redColor];
}

这是说明问题的图片

【问题讨论】:

    标签: iphone sdk uitableview accessoryview


    【解决方案1】:

    如果您想在 UITableViewCell 的自定义类中更改它,您可以使用:

    override func layoutSubviews() {
        super.layoutSubviews()
        self.backgroundColor = UIColor.whiteColor()
    }
    

    更改 awakeFromNib()init?(coder aDecoder: NSCoder) 内部的背景不起作用!

    【讨论】:

      【解决方案2】:

      在函数中

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

      使用以下代码

      cell.contentView.superview.backgroundColor = [UIColor redColor];
      

      reference for more details

      【讨论】:

        【解决方案3】:

        您需要设置 UITableViewCell 的 backgroundView 属性。例如:

        UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
        myBackgroundView.backgroundColor = [UIColor redColor];
        myCell.backgroundView = myBackgroundView;
        

        如果你想要更花哨的东西,比如红色渐变背景,你可以实现 UIView 的一个子类,它在drawRect: 中绘制渐变,并将其用作背景视图。

        如果您希望选定表格单元格的自定义外观,您还可以设置selectedBackgroundView 属性。

        【讨论】:

        • 太棒了!这很有效,而且容易得多!渐变的好主意和selectedBackgroundView 非常感谢!
        • 非常感谢...在这里卡了很长时间,您的回答是第一个解决它的人。 :)
        猜你喜欢
        • 1970-01-01
        • 2016-04-27
        • 1970-01-01
        • 2014-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-26
        • 2022-12-12
        相关资源
        最近更新 更多