0

I have a method that returns a new color each time it is called and what I want to do is be able to color the rows with a new color each time a new one is added.

With the code below I only get red rows, the blue, green and yellow are never called.

Again, what I need to do is be able to color each row with one of the four colors in the array, the first row red the second blue, the third one green the fourth one yellow and then start coloring the fifth one red again, the sixth blue and so on.

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

Any suggestion?

fs_tigre
  • 10,650
  • 13
  • 73
  • 146
  • 1
    `nextItemIndex` is an instance variable. Each time a new cell is created, it is initialized to zero. You need to make it static to make it work. – Alex Skalozub Feb 07 '16 at 02:42
  • I don't think in Swift static works the same way it works in other languages. – fs_tigre Feb 07 '16 at 02:54
  • 1
    Well, Swift sure has an awful syntax, but `static` works pretty much the same: http://stackoverflow.com/questions/26804066/does-swift-have-class-level-static-variables – Alex Skalozub Feb 07 '16 at 03:14
  • 1
    Thank you for your help but I ended up re-structuring my code to do the coloring in the `cellForRowAtIndexPath` and worked. Thank you all. – fs_tigre Feb 07 '16 at 03:27
  • 1
    It is the best aproac because awakeFromNib will not get called when tableView deques the cell.So it is better to maintain count in cellForRowAtIndexPath – Rohit Pradhan Feb 07 '16 at 04:14

3 Answers3

1

You can accomplish it in cellForRowAtIndexPath function of UITableViewCell

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
}
Jitendra
  • 842
  • 1
  • 9
  • 25
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
    }
  }
}

Create a struct that will act as your "colour dispenser". The code is relatively straightforward:

  1. You declare an integer variable that you'll use to keep track of which colour you should use next.
  2. The number variable is the currentIndex modulus 4, meaning it'll contain a value between 0 and 3.
  3. Increment the currentIndex property. This ensures that the next time you call this method you'll get a new number result.

The getNewColor method will give a new colour each time the method is called.

For instance, you can use this in your current code like so:

override func awakeFromNib() {
  super.awakeFromNib()
  let myColor = Colors.getNewColor()
  labelPrice.textColor = myColor
  labelDiscount.textColor = myColor
  labelSavings.textColor = myColor
}
Kelvin Lau
  • 6,373
  • 6
  • 34
  • 57
0

Here is how I did it that worked. Thank you all for your help.

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  
    }  
fs_tigre
  • 10,650
  • 13
  • 73
  • 146