Day11 - 修改cell背景颜色

2016-09-18  本文已影响35人  Codepgq

效果图

步骤都在图里了

<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)
        
    }

Demo - CellBackgroundColor

上一篇下一篇

猜你喜欢

热点阅读