【问题标题】:How to create a UITableViewCell with a transparent background如何创建具有透明背景的 UITableViewCell
【发布时间】:2010-11-03 17:47:42
【问题描述】:

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

知道如何实现吗?

【问题讨论】:

    标签: iphone cocoa-touch uitableview


    【解决方案1】:

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

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

    【讨论】:

    • myTable.backgroundColor = [UIColor clearColor];为我工作!谢谢
    • 这对我也有用,虽然我还必须使用:cell.backgroundColor = [UIColor clearColor];
    • 我也必须使用 cell.textLabel.backgroundColor=[UIColor clearColor]!
    • 注意:当您还想消除 UITableViewStyleGrouped 样式单元格周围的 BORDER 时,此方法有效。 (此评论是为了帮助谷歌在寻找 UITableViewStyleGrouped 和明确的时候找到这个答案。)
    • 您不必创建背景视图。默认情况下它在那里。 cell.backgroundView.background = [UIColor clearColor] 就足够了。你也可以做 self.backgroundColor = [UIColor clearColor]; ,但这可以在 Interface Builder 中设置
    【解决方案2】:

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

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

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

    【讨论】:

    • 你救了我的命。这是唯一真正有效的解决方案。
    • 如果有一个编码员奥斯卡奖,你现在就拿奥斯卡了!
    【解决方案3】:

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

    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
    • cell.contentView 的 backgroundColor 对我来说似乎是真正的关键。 (仅在 iOS7 上确认)
    • 你太棒了。如果可以的话 +100
    【解决方案4】:
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        cell.backgroundColor = [UIColor clearColor];
        cell.backgroundView.backgroundColor = [UIColor clearColor];
    }
    

    【讨论】:

    • 如果您的表格视图有背景颜色,并且您希望单元格是透明的,这样您就可以看到表格视图的背景颜色,这是正确的解决方案。
    • 这不是唯一的答案。像魅力一样工作。
    【解决方案5】:

    我有一个简单的解决方案

    Objective-c 版本:

    cell.backgroundColor = [UIColor clearColor];
    

    Swift 版本:

    cell.backgroundColor = UIColor.clearColor()
    

    【讨论】:

    • 但始终将其设置在 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    【解决方案6】:

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

    如果你想看到你的 UIView 背景,你必须让你的单元格和你的表格背景透明:

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

    马丁·马加基安

    【讨论】:

      【解决方案7】:

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

      【讨论】:

        【解决方案8】:

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

        【讨论】:

          【解决方案9】:

          有3个步骤:

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

          【讨论】:

            【解决方案10】:

            Swift 5.1

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

            【讨论】:

              【解决方案11】:

              在拔掉大部分头发后,这对于 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!
              }
              

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

              【讨论】:

                【解决方案12】:

                这是我在 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
                {
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-11-26
                  • 1970-01-01
                  • 2018-05-30
                  • 1970-01-01
                  相关资源
                  最近更新 更多