如何创建一个透明背景的UITableViewCell

75

我想将UITableViewCell的背景色设为透明,但似乎无效。在tableViewCell中有一些图像/按钮,我希望让白色grouptableview背景消失,使图像和按钮具有“浮动”效果(就好像它们不在tableview中一样)。

有什么方法可以实现这个效果吗?

13个回答

123

如果之前的两个选项都没有起作用,尝试这个

UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;

14
myTable.backgroundColor = [UIColor clearColor]; worked for me! Thanks我的表格背景颜色设置为 [UIColor clearColor],这对我有用!谢谢。 - sjobe
7
这对我也起作用了,不过我还需要使用: cell.backgroundColor = [UIColor clearColor]; - Mirko Froehlich
4
我也需要使用cell.textLabel.backgroundColor=[UIColor clearColor]! - Casebash
谢谢!这个对我有用,而 cell.backgroundView.hidden = YES 没有。 - Kevin Borders
1
你不需要创建背景视图,因为它已经默认存在了。只需将cell.backgroundView.background设置为[UIColor clearColor]即可。你也可以使用self.backgroundColor = [UIColor clearColor];,但这可以在Interface Builder中设置。 - Moose
显示剩余2条评论

45

这个问题实际上在UITableViewCell的文档中有解答。所以尽管您必须设置控件的透明度,但实际的单元格背景颜色必须按照以下方式设置。

“注意:如果您想通过设置UIView声明的backgroundColor属性更改单元格的背景颜色,则必须在委托的tableView:willDisplayCell:forRowAtIndexPath:方法中进行,而不是在数据源的tableView:cellForRowAtIndexPath:方法中进行。在分组样式表视图中更改单元格的背景颜色对iOS 3.0产生的影响与操作系统先前版本不同。它现在会影响圆角矩形内部的区域,而不是外部的区域。”

https://developer.apple.com/documentation/uikit/uitableviewcell


1
你救了我的命。这是唯一真正有效的解决方案。 - Roberto Conte Rosito
1
如果有程序员的奥斯卡学院奖,你现在肯定握着奥斯卡小金人! - Sjakelien

31

首先阅读以下文章以正确设置背景颜色:

http://howtomakeiphoneapps.com/2009/03/how-to-add-a-nice-background-image-to-your-grouped-table-view/

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中不止有一个视图被隐藏了。这会让你看到所有挡在你面前的视图。


运行完美,除了一个打字错误:setBbackgroundColor => setBackgroundColor。 - Michael Z
1
cell.contentView的背景颜色对我来说似乎是真正的关键。(仅在iOS7上确认) - bitwit
你很棒。如果可以的话,我会给你加100分。 - Katushai

27
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
}

1
如果您的表视图具有背景颜色,并且您希望单元格是透明的,以便您可以看到表视图的背景颜色,则这是正确的解决方案。 - Sam Soffes
这难道不是唯一的答案吗?运行得非常好。 - Zia

20

我有一个简单的解决方案

Objective-c 版本:

cell.backgroundColor = [UIColor clearColor];

Swift版本:

cell.backgroundColor = UIColor.clearColor()

1
但始终将其设置在 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 中。 - Sathe_Nagaraja

11

默认情况下,UITableViewCell 的背景是白色的。执行 myCell.backgroundColor = [UIColor clearColor]; 会使 UITableViewCell 变成透明,但你不会感觉到区别,因为 UITableView(UITableViewCell 的容器)也具有默认的白色背景。

如果你想看到 UIView 的背景,你需要将单元格和表格的背景都设置为透明:

myTableView.backgroundColor = [UIColor clearColor];
myTableCell.backgroundColor = [UIColor clearColor];

Martin Magakian


2

我曾经遇到这样一个问题:自定义的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];
}

2
如果您正在使用故事板,请确保取消选中原型UITableViewCells的“不透明”选项。

0
在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!
}

就是这样。当它工作时,我简直不敢相信自己的眼睛。:)


0

有3个步骤:

  1. cell.contentView.backgroundColor = UIColor.clear
  2. 在绘图部分的属性检查器中取消表视图单元格的不透明复选框。您可以通过单击Storyboard中的表视图单元格原型并打开属性检查器来找到它。向下滚动,您会找到它。
  3. 从与步骤2相同位置的下拉菜单中选择背景为Clear Color。
  4. 运行您的应用程序

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接