2016-04-25 57 views
0

我有一个像BasicCell> ContentView> Container一样的UITableView单元设置。当我在界面构建器中设置容器的背景时,我得到了所需的较暗的灰色阴影。但是,当我以编程方式设置较暗的阴影时,在cellForRowAtIndexPath中,我得到一个白色背景。无法以编程方式设置单元容器BG

cell.container.backgroundColor = UIColor(red: 20, green: 20, blue: 20, alpha: 1.0) 

因此,通过引入编程颜色设置,它突然忽略了接口构建器颜色和编程颜色。我读过几个线程,并尝试使用willDisplay cell:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    let myCell = cell as! CustomTableViewCell_F2 
    myCell.backgroundColor = UIColor.clearColor() 
    myCell.backgroundView = nil 
    myCell.container.backgroundColor = UIColor(red: 20, green: 20, blue: 20, alpha: 1.0) 
} 

我试着在CustomTableCell类中设置它。这没有用。发生了什么,我该如何解决它?

+0

试着改变你的tableView的背景颜色。如果此更改将通过单元格可见,请删除'myCell.backgroundColor = UIColor.clearColor()'行。 –

+0

我改变了表BG的颜色,它是可见的。然后,我注释了myCell.background颜色行,仍然是容器视图是白色/很浅的灰色阴影(很难说)。 –

回答

1

变化

cell.container.backgroundColor = UIColor(red: 20, green: 20, blue: 20, alpha: 1.0) 

let shade: CGFloat = 0.2 
cell.container.backgroundColor = UIColor(white: shade, alpha: 1.0) 

你的颜色UIColor(red: 20, green: 20, blue: 20, alpha: 1.0)实际上是白色的。您应该为0.0到1.0范围内的颜色分量提供值。

+0

我已经标记了它的正确性,因为它修复了它,但我从未听说过这样的事情。你能否再解释一下发生了什么以及为什么这样做能解决它? –

+0

非常简单:您使用的UIColor初始值设定项接受从0.0到1.0的值。如果您传递的值超过(例如20),它将被替换为1.0。你可以在游乐场尝试你的颜色,看它是白色的。因为你想要灰色,我决定使用更合适的初始化工具'UIColor(white:alpha:)'。 –