【发布时间】:2010-11-03 17:47:42
【问题描述】:
我正在尝试将UITableViewCell 的背景颜色设置为透明。但似乎没有任何效果。我在tableViewCell 中有一些图像/按钮,我想让白色的grouptableview 背景消失,以获得图像和按钮的“浮动”效果(就好像它们不在tableview 内一样)。
知道如何实现吗?
【问题讨论】:
标签: iphone cocoa-touch uitableview
我正在尝试将UITableViewCell 的背景颜色设置为透明。但似乎没有任何效果。我在tableViewCell 中有一些图像/按钮,我想让白色的grouptableview 背景消失,以获得图像和按钮的“浮动”效果(就好像它们不在tableview 内一样)。
知道如何实现吗?
【问题讨论】:
标签: iphone cocoa-touch uitableview
如果其他两个选项都不起作用,试试这个
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
【讨论】:
这实际上在 UITableViewCell 的文档中得到了回答。因此,虽然您必须在控件上设置透明度,但实际的单元格背景颜色必须按以下设置。
"注意:如果要更改单元格的背景颜色(通过 UIView 声明的 backgroundColor 属性设置单元格的背景颜色),必须在委托的 tableView:willDisplayCell:forRowAtIndexPath: 方法中进行而不是在数据源的 tableView:cellForRowAtIndexPath: 中。更改组样式表视图中单元格的背景颜色在 iOS 3.0 中具有与以前版本的操作系统不同的效果。现在它会影响内部区域圆角矩形而不是它之外的区域。”
https://developer.apple.com/documentation/uikit/uitableviewcell
【讨论】:
从这些文章开始正确设置背景颜色:
http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/
然后尝试以下所有行:
[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];
在 UITableViewCell 中隐藏的视图不止一个。这应该使您的方式变得清晰。
【讨论】:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView.backgroundColor = [UIColor clearColor];
}
【讨论】:
我有一个简单的解决方案
Objective-c 版本:
cell.backgroundColor = [UIColor clearColor];
Swift 版本:
cell.backgroundColor = UIColor.clearColor()
【讨论】:
默认情况下,你的 UITableViewCell 的背景是白色的。执行myCell.backgroundColor = [UIColor clearColor]; 将使您的 UITableViewCell 透明,但您不会感觉到差异,因为 UITableView(您的 UITableViewCell 的竞争者)默认情况下也有白色背景。
如果你想看到你的 UIView 背景,你必须让你的单元格和你的表格背景透明:
myTableView.backgroundColor = [UIColor clearColor];
myTableCell.backgroundColor = [UIColor clearColor];
马丁·马加基安
【讨论】:
我遇到了一个问题,我的自定义 tableviewcell 背景图像被白色标签背景覆盖,我尝试了所有方法,最后在 viewWillAppear 中设置背景以清除颜色就成功了。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.tableView.backgroundColor = [UIColor clearColor]; // if this is not here, cell background image is covered with a white rectangle :S
}
在 rowForCellAtIndexPath 中我设置了背景图片:
if(!cell) {
cell = [[[UITableViewCell alloc] initWithFrame: ...
UIImageView *cellBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbg.jpg"]];
[cell setBackgroundView:cellBG];
[cellBG release];
}
【讨论】:
如果您使用故事板,请确保取消选中原型 UITableViewCells 的“不透明”
【讨论】:
有3个步骤:
【讨论】:
Swift 5.1
的解决方案let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath)
cell.backgroundColor = UIColor.clear
【讨论】:
在拔掉大部分头发后,这对于 xCode 13 / Swift 5.1 来说非常简单。有两件事需要改变。
在你的 numberOfRowsInSection 中,添加:
tableView.backgroundColor = UIColor.clear
所以它看起来像这样:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
tableView.backgroundColor = UIColor.clear
return 40
}
然后在你的 cellForRowAt 中,添加:
cell?.backgroundColor = UIColor.clear
看起来像:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.backgroundColor = UIColor.clear
return cell!
}
就是这样。当它起作用时,简直不敢相信我的眼睛。 :)
【讨论】:
这是我在 Swift 4.1 中的解决方案:
override func viewDidLoad() {
super.viewDidLoad()
tableView.backgroundView = UIImageView(image: "Image")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell.backgroundColor = UIColor.clear
{
【讨论】: