【问题标题】:Gradient color on a particular section特定部分的渐变颜色
【发布时间】:2016-11-02 03:51:10
【问题描述】:

我有 2 个部分的 UITableView(每个部分有 n 行数)。我只需要在第一部分添加渐变颜色。有什么方法可以在 UITableView 的特定部分添加渐变色?

【问题讨论】:

  • 我的要求是不要更改部分的页眉/页脚视图。我有具有清晰颜色背景的表格视图单元格,我想在表格视图的特定部分显示渐变颜色

标签: ios swift uitableview gradient


【解决方案1】:

Apple 只允许更改页眉或页脚,而不是您想要的视图。相反,您可以为第 0 节中的每个单元格设置渐变

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let Identifier = "RWCellIdentifier"
    var cell = tableView.dequeueReusableCellWithIdentifier(Identifier)
    if (cell == nil) {
        cell = UITableViewCell(style: .Default, reuseIdentifier: Identifier)
    }
    cell?.textLabel?.text = "text test"

//gradient color
    if indexPath.section == 0 {
        let gradient: CAGradientLayer = CAGradientLayer()
        gradient.frame = CGRectMake(0, 0, self.view.frame.width, cell!.frame.height)
        gradient.colors = [UIColor.redColor().CGColor, UIColor.yellowColor().CGColor]
        cell!.layer.insertSublayer(gradient, atIndex: 0)
    }
    return cell!
}

【讨论】:

    【解决方案2】:

    您可以只比较要显示渐变颜色的部分。示例:

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
        let view: UIView = UIView(frame: CGRectMake(0.0, 0.0, 320.0, 50.0))
    
        if section == 0 {  // for your gradient section. it can be any section that you want
    
           let gradient: CAGradientLayer = CAGradientLayer()
           gradient.frame = view.bounds
           gradient.colors = [UIColor.whiteColor().CGColor, UIColor.blackColor().CGColor]
           view.layer.insertSublayer(gradient, atIndex: 0)
    
        } else {
           view.backgroundColor = UIColor.lightGrayColor()
    
        }
        return view
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      相关资源
      最近更新 更多