如何创建具有透明背景的UITableViewCell

时间:2009-06-17 16:43:56

标签: iphone cocoa-touch uitableview

我正在尝试将UITableViewCell的背景颜色设置为透明。但似乎没有任何效果。我在tableViewCell中有一些图像/按钮,我想让白色grouptableview背景消失,以获得图像和按钮的“浮动”效果(好像它们不在{{1}内1}})。

知道如何实现这一目标吗?

11 个答案:

答案 0 :(得分:121)

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

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

答案 1 :(得分:42)

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

  

“注意:如果要更改单元格的背景颜色(通过UIView声明的backgroundColor属性设置单元格的背景颜色),则必须在tableView中执行此操作:willDisplayCell:forRowAtIndexPath:委托方法而不是在tableView:cellForRowAtIndexPath:数据源。对于组样式表视图中单元格的背景颜色的更改在iOS 3.0中具有与以前版本的操作系统不同的效果。它现在影响到圆角矩形而不是它外面的区域。“

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

答案 2 :(得分: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中隐藏的视图不止一个。这应该使你所有的方式都清楚。

答案 3 :(得分:25)

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

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

答案 4 :(得分:18)

我有一个简单的解决方案

Objective-c 版本:

cell.backgroundColor = [UIColor clearColor];

Swift 版本:

cell.backgroundColor = UIColor.clearColor()

答案 5 :(得分:10)

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

如果你想看到你的UIView背景,你必须让你的细胞和你的桌子背景透明:

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

Martin Magakian

答案 6 :(得分:2)

我有一个问题,我的自定义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];
}

答案 7 :(得分:2)

如果您正在使用故事板,请务必取消选中原型UITableViewCells的“Opaque”

答案 8 :(得分:0)

共有3个步骤:

  1. cell.contentView.backgroundColor = UIColor.clear
  2. 在“图形”部分的属性检查器中取消选中表视图单元格的不透明复选框。您可以通过在情节提要中单击表格视图单元原型并打开属性检查器来找到。向下滚动,您将找到它。
  3. 从与步骤2相同的位置的下拉菜单中选择“背景”作为“清除颜色”。
  4. 运行您的应用

答案 9 :(得分:0)

Swift 5.1

的解决方案
let cell = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier", for: indexPath)
cell.backgroundColor = UIColor.clear

答案 10 :(得分:-1)

这是我在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
{
相关问题