美文网首页
Day11 - 修改cell背景颜色

Day11 - 修改cell背景颜色

作者: Codepgq | 来源:发表于2016-09-18 10:10 被阅读35次

    效果图

    步骤都在图里了

    <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

    相关文章

      网友评论

          本文标题:Day11 - 修改cell背景颜色

          本文链接:https://www.haomeiwen.com/subject/qxsbettx.html