【发布时间】:2016-05-13 11:26:49
【问题描述】:
我在 CustomCell 中有一个 UILabel,我想在每次将它添加到表格视图时用不同的颜色绘制这个标签。
let myBlue = UIColor(red: 62.0/255, green: 174.0/255, blue: 206.0/255, alpha: 1.0)
let myGreen = UIColor(red: 110.0/255, green: 186.0/255, blue: 64.0/255, alpha: 1.0)
let myRed = UIColor(red: 247.0/255, green: 118.0/255, blue: 113.0/255, alpha: 1.0)
let myYellow = UIColor(red: 255.0/255, green: 190.0/255, blue: 106.0/255, alpha: 1.0)
myLabel.backgroundColor = random(myBlue, myRed, myGreen, myYellow)
有什么建议吗?
谢谢
【问题讨论】:
-
myLabel.backgroundColor = [myBlue, myRed, myGreen, myYellow][Int(arc4random_uniform(4))]或[myBlue, myRed, myGreen, myYellow][arc4random_uniform(4).hashValue] -
如果你想要一个方法
func randomColor(colors: UIColor...) -> UIColor { return colors[Int(arc4random_uniform(UInt32(colors.count)))] } -
非常感谢您的帮助。