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

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

任何想法如何可以完成?

如果其他选项都不起作用,请尝试此操作

 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中有一个作用,与以前版本的操作系统不同,现在影响了圆angular矩形内的区域在它之外的区域“。

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

从这些文章开始,以正确设置背景颜色:

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中的不止一个视图。 这应该使你的方式清楚所有的。

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor = [UIColor clearColor]; cell.backgroundView.backgroundColor = [UIColor clearColor]; } 

我有一个简单的解决scheme

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]; 

马丁·马加基安

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

我有一个问题,我的自定义tableviewcell backgroundimage与白色标签背景叠加,我试了一切,最后设置背景清除颜色在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]; }