效果图
<br />
只粘贴部分代码:
通过bool判断是否要改变颜色
private func cellBackgroundColor(indexPath : NSIndexPath) -> UIColor{
if !change {
return UIColor.whiteColor()
}
let green : CGFloat = CGFloat(indexPath.row) / CGFloat(data.count)
return UIColor(red: 1.0, green: green, blue: 0, alpha: 1)
}
<br />
重写willDisplayCell
方法
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath){
cell.backgroundColor = cellBackgroundColor(indexPath)
}
<br />
添加渐变层方法:
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
gradientLayer.frame = self.bounds
let color1 = UIColor(white: 1.0, alpha: 0.2).CGColor as CGColorRef
let color2 = UIColor(white: 1.0, alpha: 0.1).CGColor as CGColorRef
let color3 = UIColor.clearColor().CGColor as CGColorRef
let color4 = UIColor(white: 0.0, alpha: 0.05).CGColor as CGColorRef
gradientLayer.colors = [color1, color2, color3, color4]
gradientLayer.locations = [0.0, 0.04, 0.95, 1.0]
layer.insertSublayer(gradientLayer, atIndex: 0)
}