【问题标题】:Swift: gradient on a cell of a tableviewSwift:表格视图单元格上的渐变
【发布时间】:2014-10-22 18:38:03
【问题描述】:

在 tableView 的单元格上,我正在尝试使用以下 Swift 代码定义渐变:

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

    var cell:UITableViewCell = self.tableView?.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    cell.backgroundColor=UIColor.clearColor()
    let gradient : CAGradientLayer = CAGradientLayer()
    var arrayColors:Array<AnyObject> = [UIColor.blackColor().CGColor, UIColor.whiteColor().CGColor]
    gradient.colors=arrayColors
    gradient.frame = cell.bounds
    cell.layer.insertSublayer(gradient, atIndex: UInt32(indexPath.row))

    return cell
 }

行为非常混乱:第一次加载视图时,所有单元格都有白色背景。如果我上下滚动,一些内容的单元格消失(标题和副标题),渐变最终出现在这些单元格中(随机的,其他一些单元格保持白色背景)。 是否与此演员表有关 -> UInt32(indexPath.row) ?

【问题讨论】:

  • 尝试使用@IBOutlet var table 添加您的tableview:UITableView!并在你的 func tableView 添加 table.reloadData()
  • 看看这个:github.com/rlease/GradientTableExample - 发现上次我正在寻找类似的东西。也许有帮助。
  • 您可能忘记明确定义颜色的locations。我知道这只是可选的,但为了安全起见,你知道;您也不需要向重复使用的单元格添加新的渐变层,该单元已经具有渐变层;并且该层应该插入到第 0 个索引中,我不明白您为什么尝试将其插入到 rowth 索引处。那有什么意义呢?

标签: ios swift tableview gradient cagradientlayer


【解决方案1】:

我遇到了类似的问题,我的错误是直接在单元格中插入带有渐变的新图层,尝试将新图层插入到 backgroundView 类似这样的东西

let gradient = CAGradientLayer()
// layer setup...
cell.backgroundView = UIView()
cell.backgroundView?.layer.insertSublayer(gradientLayer, atIndex: 0)

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    渐变是一种视觉装饰。它特定于细胞类。细胞的外观是细胞的责任。然后应该在单元类中定义它。它绝对不属于 cellForRowAtIndexPath,它应该只是为正确的行选择正确的单元格。 但是您在该方法中配置单元的内部结构,再加上排队的工作方式,会导致所有问题。

    1) 创建 UITableViewCell 的子类(以编程方式,IB ..这并不重要)

    2) 将 awakeFromNib 或 layoutSubviews 中的图层添加到 backroundView 属性中。

    3) 在 tableViewController 的 viewDidLoad 中注册单元格。

    【讨论】:

      【解决方案3】:

      进一步扩展 Earl Grey 和 hidra Answer,

      当您向特定视图添加渐变时,您需要为渐变层设置实际框架。

      如果您添加了最初加载的渐变层的视图确实加载了,那么您必须将其放入,然后它不会按预期调整大小,您可以通过两种方式解决此问题。

      首先: 通过计算视图的大小手动给定框架,但这非常忙碌,因为您必须为每个 iPhone 版本计算您可以计算宽度和高度比 BY。

       let bounds = UIScreen.main.bounds
       let deviceheight = bounds.size.height
       let deviceWidth = bounds.size.width
      

      然后你计算并给出宽度大小

      另一种方法是在加载视图后添加图层,无需计算即可:

       override func viewDidAppear(_ animated: Bool) {
          Your gradient code //you can create a method for adding a gradient layer 
      }
      

      现在在 Tableview 或集合视图中存在同样的问题,我们没有任何视图确实出现在单元格中。我们有两种情况。

      1. 数据是静态的。
      2. 数据来自服务器

      当数据是静态的时,你必须做的第一个场景是创建一个布尔类型的变量,并使其为假,直到加载视图。然后让它在视图中确实出现在索引方法中的行方法并再次重新加载单元格。

      class VC: UIViewController {
      
      
      var isCellLoaded : Bool = false
      
      override func viewDidAppear(_ animated: Bool) {
      
          isCellLoaded = true
          self.aTable.reloadData()
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
      
      
          let cell = tableView.dequeueReusableCell(withIdentifier: "youcellidentifier", for: indexPath) as! youcell 
          if isCellLoaded {
              cell.gradientView.setGradient(cornerRadius: 5, gradientLeftColor: .notificationViewLeftColor, gradientRightColor: .notificationViewRightColor)
              if let gradient = cell.gradientView.layer.sublayers?[0] as? CAGradientLayer {
                  gradient.frame = cell.gradientView.bounds
              }
          }
      
          return cell
      }
      

      数据来自 API 的第二种情况

      万岁!!你不需要做任何事情,因为第一次直到 api 中没有数据没有给出任何响应,当响应到来时你必须重新加载数据。一切都会完美无缺。 块引用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-24
        • 1970-01-01
        • 1970-01-01
        • 2016-11-23
        • 1970-01-01
        • 2018-02-26
        • 1970-01-01
        相关资源
        最近更新 更多