2016-04-25 35 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:)'。 –

相關問題