189

我已经实现了一个自定义表格视图单元类,它继承自UITableViewCell. tableview 包含一个背景图像,所以我希望单元格的背景是透明的。在iOS7之前看起来很棒。

但是,在 iOS7 中,单元格始终以白色背景显示。


即使对于 Xcode7, 2015,storyboard 中也存在一个错误:您必须在代码中设置单元格的背景颜色。

4

14 回答 14

385

正如 Apple DOC 所说(UITableViewCell 类参考):

...在 iOS 7 中,单元格默认为白色背景;在早期版本的 iOS 中,单元格继承了封闭表格视图的背景颜色。如果要更改单元格的背景颜色,请在 tableView 委托的tableView:willDisplayCell:forRowAtIndexPath:方法中进行。

因此,对于我的情况,要显示具有透明背景的单元格,只需在表格视图控制器中实现委托方法,如下所示:

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

请注意:正如@null 所说,“...界面生成器中似乎存在错误...”,我不完全确定它是否确实存在错误,但似乎是因为他的评论得到了几票赞成。因此,如果您使用 IB,可能会出现问题。:)

于 2013-09-18T17:08:20.460 回答
67

我稍微调查了一下,发现单元格背景颜色是由外观系统设置的。因此,如果您的应用程序中的所有单元格都有清晰的背景,最简单的解决方案是:

[[UITableViewCell appearance] setBackgroundColor:[UIColor clearColor]];

即使他们有不同的背景,清晰的颜色似乎是最方便的默认颜色。

于 2013-11-29T16:44:06.057 回答
30

在返回单元格之前将其写在您的 cellForRowAtIndexPath 方法中;

cell.backgroundColor = [UIColor clearColor];
于 2013-10-21T10:22:54.820 回答
14

UITableViewCell在 iOS 7 中默认的背景颜色是白色。

您必须backgroundColor在代码中的某处设置该属性。例如,在新创建单元格后设置它。

cell.backgroundColor = [UIColor clearColor];
于 2013-09-24T09:44:53.157 回答
13

我实际上发现问题是由于 UITableView 的背景颜色没有设置为清除。如果您将 UITableViewCell 的背景颜色更改为清除并且您发现您仍然看到白色,请确保将 UITableView 的背景颜色设置为清除(或任何您想要的)。

[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor clearColor]];

在斯威夫特

tableView.backgroundView = nil
tableView.backgroundColor = UIColor.clearColor()
于 2013-11-15T04:12:12.943 回答
10

这绝对是一个 iOS 错误。在 iOS 8 中,在 iPhone 上设置背景颜色Interface Builder可以正常工作,但在 iPad 中总是whiteColor. 要修复它,在cellForIndexPath数据源消息中,将其放入:

cell.backgroundColor = cell.backgroundColor

它会起作用。这正是我说这是一个错误的原因,因为代码本身几乎是胡说八道,但它确实有效。

于 2015-04-19T04:24:41.963 回答
4

可能是您的 UITableViewCell 默认被选中。您可以使用 Xcode 6s 新的可视化调试器确认这一点(或找出导致此白色单元格出现的确切视图)。

在此处输入图像描述

有趣的是,在知道这一点之后.. 将所选单元格的背景颜色设置为清除仍然不起作用.. 做更多的研究,结果发现我在创建这个自定义单元格时只需要修改选择样式:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // do customization here
    }
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self setBackgroundColor:[UIColor clearColor]];
    return self;
}
于 2015-01-31T10:44:35.627 回答
3

我发现上述方法都不适用于 XCode 5.1.1、iOS 7.1。

如果您使用带有原型单元格的界面生成器,请选择原型单元格,然后从视图部分的属性选择器中,将背景表单默认更改为清除颜色:

在此处输入图像描述

这似乎工作正常。也不需要上述代码更改——这是一个纯 IB 解决方案。

于 2014-08-18T20:13:53.087 回答
2

接受的答案并没有为我解决问题。我不得不这样做:

  1. 在界面生成器中,选择表视图。然后从属性检查器,从视图部分,将背景设置为透明(0% 不透明度)。

在此处输入图像描述

  1. 从表的数据源类 cellForRowAtIndexPath 方法:

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
        reuseIdentifier:CellIdentifier];
    cell.backgroundColor = [UIColor clearColor]; //Make cell transparent
    
于 2013-10-28T16:36:29.133 回答
2

斯威夫特 1.2 解决方案:

只需为单元格的背景颜色添加一个明确的定义,如下所示:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // Configure the cell...
    cell.backgroundColor = UIColor.clearColor()

    return cell
}
于 2015-05-12T23:36:44.983 回答
2

对我来说, cell.backgroundColor = cell.contentView.backgroundColor; 要么投入工作,tableView:willDisplayCell:forRowAtIndexPath:要么tableView:cell:forRowAtIndexPath:完成工作。

这是因为我根据需要在 Interface Builder 中设置了 contentView 的背景颜色。

于 2015-11-02T13:01:55.147 回答
1

将 UI 对象添加到单元格时,Xcode 5/IOS 7 添加了一个新的“内容视图”,它将成为单元格中所有元素的超级视图。要设置单元格的背景颜色,请设置此内容视图的背景颜色。上面的解决方案对我不起作用,但这个对我来说效果很好。

于 2013-10-12T14:13:39.883 回答
1

有类似的问题,不得不

  1. 将蓝色设置为 selectionStyle 和
  2. 将此添加到代码中以更正它

    override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    
      var bgColorView: UIView = UIView()
      bgColorView.backgroundColor = UIColor(red: (76.0 / 255.0), green: (161.0 / 255.0), blue: (255.0 / 255.0), alpha: 1.0)
      bgColorView.layer.masksToBounds = true
      tableView.cellForRowAtIndexPath(indexPath)!.selectedBackgroundView = bgColorView
      return true
    }
    
于 2016-04-29T09:45:07.400 回答
0

您还可以使用颜色代码来使您的 UITableView Cell 有利可图。

[cell setBackgroundColor:[self colorWithHexString:@"1fbbff"]];

这是应用颜色代码方法的代码:

-(UIColor*)colorWithHexString:(NSString*)hex
{
    NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];


    if ([cString length] < 6) return [UIColor grayColor];

    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];

    if ([cString length] != 6) return  [UIColor grayColor];

    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];

    range.location = 2;
    NSString *gString = [cString substringWithRange:range];

    range.location = 4;
    NSString *bString = [cString substringWithRange:range];

    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return [UIColor colorWithRed:((float) r / 255.0f)
                         green:((float) g / 255.0f)
                          blue:((float) b / 255.0f)
                         alpha:1.0f];
}
于 2013-11-02T20:25:08.400 回答