【问题标题】:How to call a function each time a new tableViewCell is added to a UITableView每次将新 tableViewCell 添加到 UITableView 时如何调用函数
【发布时间】:2016-02-07 02:39:08
【问题描述】:

我有一个方法,每次调用它时都会返回一种新颜色,我想做的是每次添加新颜色时都能用新颜色为行着色。

使用下面的代码我只得到red 行,蓝色、绿色和黄色永远不会被调用。

再次,我需要做的是能够用数组中的四种颜色之一为每一行着色,第一行 red 第二行 blue,第三行 green 第四行 @987654326 @ 然后开始再次为第五个red 着色,第六个blue 等等。

class CustomCell: UITableViewCell {
    let myColors = [colorMyRed, colorMyBlue, colorMyGreen, colorMyYellow]

    var nextItemIndex = 0

    func color() -> UIColor {
        let result = myColors[nextItemIndex]
        nextItemIndex = nextItemIndex + 1
        return result
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        // how can I assign a new color to this variable 
        // each time awakeFromNib is called
        let myColor:UIColor = color()

        // these labels need to be colored with one of the
        // four colors each time a new row is added  
        labelPrice.textColor = myColor
        labelDiscount.textColor = myColor
        labelSavings.textColor = myColor
    }
}

有什么建议吗?

【问题讨论】:

  • nextItemIndex 是一个实例变量。每次创建新单元格时,都会将其初始化为零。您需要将其设为静态才能使其正常工作。
  • 我认为在 Swift 中静态的工作方式与在其他语言中的工作方式不同。
  • 好吧,Swift 的语法确实很糟糕,但 static 的工作原理几乎相同:stackoverflow.com/questions/26804066/…
  • 感谢您的帮助,但我最终重新构建了我的代码以在 cellForRowAtIndexPath 中进行着色并工作。谢谢大家。
  • 这是最好的aproac,因为当tableView deques cell时不会调用awakeFromNib。所以最好在cellForRowAtIndexPath中保持计数

标签: ios swift uitableview


【解决方案1】:

您可以在 UITableViewCell 的 cellForRowAtIndexPath 函数中完成

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

    let cell = tableView.dequeueReusableCellWithIdentifier("CellIndentifier") as UITableViewCell!
    // Configure the cell...
    switch(indexPath.row) {
    case 0, 4, 8, 12:
        cell.textLabel!.textColor = UIColor.redColor()
        cell.textLabel!.text = "Red"
        break
    case 1, 5, 9, 13:
        cell.textLabel!.textColor = UIColor.blueColor()
        cell.textLabel!.text = "Blue"
        break
    case 2, 6, 10, 14:
        cell.textLabel!.textColor = UIColor.greenColor()
        cell.textLabel!.text = "Green"
        break
    case 3, 7, 11, 15 :
        cell.textLabel!.textColor = UIColor.yellowColor()
        cell.textLabel!.text = "Yellow"
        break
    default:
        cell.textLabel!.textColor = UIColor.blackColor()
        cell.textLabel!.text = "Black"
        break
    }
    return cell
}

【讨论】:

    【解决方案2】:
    private struct Colors {
      static var currentIndex = 0 // 1
      static func getNewColor() -> UIColor { 
        let number = Colors.currentIndex % 4 // 2
        Colors.currentIndex += 1 // 3
        switch number { // 4
          case 0:
            return UIColor.redColor()
          case 1:
            return UIColor.blueColor()
          case 2:
            return UIColor.greenColor()
          case 3:
            return UIColor.yellowColor()
          default:
            fatalError() // should not encounter this case
        }
      }
    }
    

    创建一个结构来充当您的“颜色分配器”。代码比较简单:

    1. 您声明了一个整数变量,您将使用该变量来跟踪您接下来应该使用哪种颜色。
    2. number 变量是 currentIndex 模数 4,这意味着它将包含一个介于 0 和 3 之间的值。
    3. 增加currentIndex 属性。这样可以确保下次调用此方法时,您将获得一个新的number 结果。

    getNewColor 方法将在每次调用该方法时赋予新的颜色。

    例如,您可以像这样在当前代码中使用它:

    override func awakeFromNib() {
      super.awakeFromNib()
      let myColor = Colors.getNewColor()
      labelPrice.textColor = myColor
      labelDiscount.textColor = myColor
      labelSavings.textColor = myColor
    }
    

    【讨论】:

      【解决方案3】:

      这是我如何做到的。谢谢大家的帮助。

      CustomCell.swift

      class CustomCell: UITableViewCell {  
      
          var labelColor: UIColor! {  
              get {  
                  return labelPrice.textColor  
              }  
              set {  
                  labelPrice.textColor = newValue  
                  labelDiscount.textColor = newValue  
                  labelSavings.textColor = newValue  
              }  
          }  
      }
      

      ViewController.swif (***cellForRowAtIndexPath*)**

          let myColors = [colorMyRed, colorMyBlue, colorMyGreen, colorMyYellow]  
      
          func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {  
              let myCustomCell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell  
      
              let nextItemIndex = indexPath.row % myColors.count  
              myCustomCell.labelColor = myColors[nextItemIndex]  
              return myCustomCell  
          }  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-06
        • 2014-12-06
        • 2021-07-14
        • 1970-01-01
        • 2017-02-16
        相关资源
        最近更新 更多