每次将新的tableViewCell添加到UITableView时,如何调用函数

时间:2016-02-07 02:39:09

标签: ios swift uitableview

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

使用下面的代码我只获得red行,从不调用蓝色,绿色和黄色。

同样,我需要做的是能够使用数组中的四种颜色之一为每一行着色,第一行red第二行blue,第三行{{1}第四个green,然后再次开始着色第五个yellow,第六个red,依此类推。

blue

有什么建议吗?

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:1)

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结果。
  4. 每次调用方法时,getNewColor方法都会给出一种新颜色。

    例如,您可以在当前代码中使用它,如下所示:

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

答案 2 :(得分:0)

以下是我的工作方式。谢谢大家的帮助。

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