UITableViewCell显示白色背景,不能在iOS7上修改

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

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


即使是2015年的Xcode7, 故事板中有一个错误 :你必须在代码中设置单元格的背景颜色。

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

在iOS 7中,默认情况下,单元格具有白色背景 ; 在早期版本的iOS中,单元格inheritance封闭表视图的背景颜色。 如果要更改单元格的背景颜色,请在表视图委托的tableView:willDisplayCell:forRowAtIndexPath:方法中执行此操作。

所以对于我的情况来说,显示具有透明背景的单元格,只需要像下面这样在表视图控制器中实现委托方法:

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

注意 :@null说: “…似乎有一个接口生成器的错误…” ,我不完全确定它是否有错误,但似乎是如此,因此他的评论得到了多个票。 所以如果你使用IB,可能会出现问题。 🙂

我调查了一下,发现单元格backgroundColor由外观系统设置。 因此,如果应用程序中的所有单元格都具有清晰的背景,则最简单的解决scheme是:

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

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

在返回单元格之前在你的cellForRowAtIndexPath方法中写这个;

 cell.backgroundColor = [UIColor clearColor]; 

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

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

在Swift中

 tableView.backgroundView = nil tableView.backgroundColor = UIColor.clearColor() 

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

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

 cell.backgroundColor = [UIColor clearColor]; 

这绝对是一个iOS的错误。 在iOS 8中,在Interface Builder设置背景色可以很好地适用于iPhone,但在iPad中,它始终是whiteColor 。 要修复它,在cellForIndexPath数据源消息中,把它放在:

cell.backgroundColor = cell.backgroundColor

它会工作。 这就是为什么我说这是一个错误,因为代码本身是非常糟糕的,但它的工作原理。

这可能是因为你的UITableViewCell是默认选中的。你可以使用Xcode 6s新的可视化debugging器来确认这个(或者确切的找出导致这个白色单元出现的视图)。

在这里输入图像描述

有趣的是,知道了这一点后,设置所选单元格的背景颜色清除仍然没有工作..做更多的研究,事实certificate,我只是不得不修改select样式,当我创build这个自定义单元格:

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

我发现以上都没有从XCode 5.1.1,iOS 7.1工作。

如果将Interface Builder与原型单元一起使用,请select原型单元,然后从“视图”部分的属性select器中将“背景”窗体默认设置为“清除颜色”:

在这里输入图像描述

这似乎工作正常。 上述代码变更都不需要,这是一个纯粹的IB解决scheme。

接受的答案并没有解决我的问题。 我必须这样做:

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

在这里输入图像描述

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

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.backgroundColor = [UIColor clearColor]; //Make cell transparent 

对我来说设置cell.backgroundColor = cell.contentView.backgroundColor; 无论是在tableView:willDisplayCell:forRowAtIndexPath:tableView:cell:forRowAtIndexPath:做的工作。

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

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

Swift 1.2解决scheme:

只要为你的单元格的背景色添加一个明确的定义就像这样:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // Configure the cell... cell.backgroundColor = UIColor.clearColor() return cell } 

有一个类似的问题,不得不

  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 } 

您也可以使用颜色代码,以使您的UITableView细胞丰厚。

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