2009-06-17 55 views
68

我试图将UITableViewCell的背景颜色设置为透明。但似乎没有任何工作。我在tableViewCell中有一些图像/按钮,我想让背景消失以获得图像和按钮的“浮动”效果(就好像它们不在tableview中一样)。如何使用透明背景创建UITableViewCell

任何想法如何实现?

回答

120

如果两个其他选项不起作用试试这个

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

myTable.backgroundColor = [UIColor clearColor];为我工作!谢谢 – sjobe 2009-11-05 14:21:14

+7

这也适用于我,虽然我还必须使用: cell.backgroundColor = [UIColor clearColor]; – 2009-11-10 07:27:07

30

开始与这些文章,以获得背景色正确设置:

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中的视图。这应该使你的方式清晰。

+0

作品完美... 除了一个错字:setBbackgroundColor => setBackgroundColor – 2010-05-03 22:40:04

+1

cell.contentView的backgroundColor对我来说似乎是真正的关键。 (仅在iOS7上确认) – bitwit 2014-02-28 23:00:24

+0

你真棒。 +100如果我可以 – Katushai 2014-06-03 21:03:54

2

我在我的自定义tableviewcell和backgroundImage用白色标签背景叠加的问题,我都试过了,最后设置背景色清在看法会有所斩获。

 - (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]; 
} 
40

这UITableViewCell中的文档实际上是回答。因此,虽然您必须在控件上设置透明度,但实际单元格背景颜色必须在每个下方设置为

“注意:如果你想改变一个单元格的背景色(通过设置经由UIView的声明backgroundColor属性单元格的背景颜色),你必须这样做在的tableView:willDisplayCell:forRowAtIndexPath:方法,而不是在数据源的tableView:cellForRowAtIndexPath:中改变组风格表格视图中单元格的背景颜色在iOS 3.0中的作用与以前版本的操作系统不同,它现在影响圆角矩形内的区域,而不是其外部的区域。“

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

11

默认情况下你的UITableViewCell的背景是白色的。做myCell.backgroundColor = [UIColor clearColor];会使你的UITableViewCell透明,但你不会感觉到差异,因为UITableView(你的UITableViewCell的竞争者)在默认情况下也有白色背景。

如果你想看看你的UIView的背景,你将不得不让你的细胞,你的表格背景透明:

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

马丁Magakian

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

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

我有一个简单的解决方案

Objective-c版本:

cell.backgroundColor = [UIColor clearColor]; 

斯威夫特版本:

cell.backgroundColor = UIColor.clearColor() 
3

如果您使用的故事板,确保取消选中 “不透明” 为原型UITableViewCells