14

我正在尝试设置我的 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];
}

这是说明问题的图像

UIAccessoryView 不会改变背景颜色。

4

3 回答 3

46

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

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

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

selectedBackgroundView如果您想为选定的表格单元格设置自定义外观,您也可以设置该属性。

于 2011-03-19T10:20:04.820 回答
4

在功能

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

使用以下代码

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

更多细节参考

于 2012-11-29T12:49:08.033 回答
0

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

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

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

于 2016-04-10T01:51:44.483 回答