UITableView backgroundColor在iPad上始终为白色
我正在做一个项目。 我有很多UITableView被设置为清晰的颜色。 他们的意见的背景颜色设置为我的自定义颜色,一切都很好,在iPhone上 。 
 这个问题出现在iPad上 ! 我尝试了几乎所有的东西,但我的UITableView有一个白色的颜色。 
我检查了其他主题,例如: UITableView backgroundColor在iPad上总是灰色 ,但没有任何工作。 另外,我的问题不是灰色的,它像雪一样白。
可能是什么原因呢?
好消息:根据发行说明,对于iOS 10:
在iPad上运行时,故事板中为UITableViewCell设置的背景色现在得到了遵守。
对于<10:
我在iOS 8(8.3)中看到了这一点。 即使在IB,我的细胞是“清晰的颜色”,他们的内容意见是“清晰的颜色”,他们会呈现白色。 一个不完善但合理的解决scheme,因为它仍然需要IB的价值:
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... cell.backgroundColor = cell.contentView.backgroundColor; return cell; } 
我的出队重复使用的单元格似乎在iPad上被强制为白色。 我能够使用视图层次结构debugging器来确定这一点。
一旦我这样做了,我就可以使用表格的背景颜色,并且不必设置背景视图,尽pipe这也适用。
你可以通过在appDelegate文件中创build一个外观API设置来解决这个问题:
迅速:
 UITableViewCell.appearance().backgroundColor = UIColor.clearColor() 
而不是设置背景颜色,而是尝试使用背景视图,如下所示:
 - (void)viewDidLoad { self.tableView.backgroundView = [UIView new]; self.tableView.backgroundView.backgroundColor = [UIColor clearColor]; } 
我有问题的地方使用backgroundColor不总是产生效果,但设置一个背景视图,而不是工作正常。
build立closures本弗林的答案… cell.contentView背景颜色不一定等于cell.background颜色。 在这种情况下,我发现这在更多的情况下解决了iPad的白色背景问题:
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... cell.backgroundColor = cell.backgroundColor; return cell; } 
虽然这个说法看起来荒谬而疯狂,但它解决了这个问题。
我有一个桌子里面有很多不同的单元格,每个都有不同的颜色。
  @Ben Flynn的答案cell.backgroundColor = self.contentView.backgroundColor无法实现。 原因是self.contentView.backgroundColor是零,所以你所做的只是清除cell.backgroundColor = nil 。 
 基本上这是从Xcode的错误(我认为,是的,它太烂了!), cell.backgroundColor仍然有颜色,但它不能显示。 
经过一段时间的debugging,基于@Ray W的答案,这里是我的解决scheme。
 @impelement YouCustomCellClass - (void)awakeFromNib { [super awakeFromNib]; self.backgroundColor = self.backgroundColor; // What the heck?? But it is. } @end 
 根据我的经验,iOS的一些版本在调用委托的tableView:willDisplayCell:forRowAtIndexPath:之前设置了UITableViewCell的backgroundColor tableView:willDisplayCell:forRowAtIndexPath: 在该方法中重置为您的自定义颜色可以修复它。 
这为我解决了这个问题
  func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { tableView.backgroundColor = UIColor.clear } 
SWIFT 3.XX
把这个
 UITableViewCell.appearance().backgroundColor = UIColor.clear 
 在AppDelegate 
 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
 我正在使用Storyboard与UITableViewControlrs ,所以最简单的决定是所有控制器的子类,并将此方法添加到父 
 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){ cell.backgroundColor = [UIColor clearColor]; } } 
迅速
这也是我发生的事情。 我的SWRevealViewController中的表格视图在我的iPhone上显示为白色,当它在我的iPhone上看起来很清晰(这是我想要的背景图像)。 我尝试了以上所有,但是这是在我的viewDidLoad()结束了我的工作。
 tableView.backgroundView = UIImageView(image: UIImage(named: "gray")) tableView.backgroundView?.backgroundColor = .clearColor() UITableViewCell.appearance().backgroundColor = .clearColor() 
Swift 3.1
 我已经解决了这个错误,把它放在我的UITableViewCell子类中: 
从NIB文件加载单元格时:
 override func awakeFromNib() { super.awakeFromNib() self.backgroundColor = UIColor.clear } 
当这个单元被系统重新使用时
 override func prepareForReuse() { super.prepareForReuse() self.backgroundColor = UIColor.clear } 
animeshporwal说,iOS 10应该在Interface Builder中解决这个问题。
这可以在Storyboard中实现如下:
- 显示故事板的文档大纲
- 在你的表中,select一个TableViewCell
- 转到该单元格中的内容视图
- 设置内容视图的背景颜色。
结果:iPad和iPhone模拟器视图看起来一样
我只是有这个问题,并通过更改视图(而不是tableView)的backgroundColor来修复它。 似乎在iPad上,这是首先使用的,在我的情况下,它被设置为白色。
SWIFT我也有这个问题。 在.phone上正常工作,但tableViewCells在.pad上变白。 以为我会展示如何使用swift修复它。
将tableViewCell作为@IBOutlet连接到viewController.swift,如下所示:
  @IBOutlet weak var tvc1: UITableViewCell! @IBOutlet weak var tvc2: UITableViewCell! @IBOutlet weak var tvc3: UITableViewCell! 
然后在viewDidLoad中放入以下内容:
 tvc1.backgroundColor = tvc1.backgroundColor tvc2.backgroundColor = tvc2.backgroundColor tvc3.backgroundColor = tvc3.backgroundColor 
很奇怪,我不知道这里发生了什么,但是这个解决了我的问题。
 这似乎是在UIKit笔记下的发行说明中提到的iOS 10 Beta 4中解决的: 
  
 
我有一个透明的表格视图与半透明表格视图单元格。 我创build表格时,将表格视图背景颜色设置为清除。 这适用于除iPad + iOS 8之外的所有iOS /设备组合,背景色为白色。
 对于我来说,设置单元格的背景颜色为半透明,内容视图背景颜色清除工作,因为我在每个tableView(_ tableView: UITableView, cellForRowAt indexPath) 。 对我来说问题只是表视图背景保持白色。 
 我尝试了所有关于这个问题的组合,但是我实际需要的唯一的事情就是将table view的背景设置为在每个tableView(_ tableView: UITableView, cellForRowAt indexPath)上透明。 不是超级直观的,但至less是有效的。  :P