首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单元格背景和附件

单元格背景和附件
EN

Stack Overflow用户
提问于 2011-03-19 18:05:10
回答 3查看 5.8K关注 0票数 14

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

有没有人能帮我把UIAccessoryView的背景颜色改成透明的(或者其他颜色)?

这是我的代码

代码语言:javascript
复制
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];
}

这是一张说明这个问题的图片

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-19 18:20:05

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

代码语言:javascript
复制
UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
myBackgroundView.backgroundColor = [UIColor redColor];
myCell.backgroundView = myBackgroundView;

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

如果需要选定表单元格的自定义外观,也可以设置selectedBackgroundView属性。

票数 46
EN

Stack Overflow用户

发布于 2012-11-29 20:49:08

在函数中

代码语言:javascript
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

使用以下代码

代码语言:javascript
复制
cell.contentView.superview.backgroundColor = [UIColor redColor];

reference for more details

票数 4
EN

Stack Overflow用户

发布于 2016-04-10 09:51:44

如果你想在UITableViewCell的自定义类中修改它,你只需要使用:

代码语言:javascript
复制
override func layoutSubviews() {
    super.layoutSubviews()
    self.backgroundColor = UIColor.whiteColor()
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5361408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档