3

Can anyone see why I can't set the background color property of my tableView cells to colors from the array I created in ViewDidLoad()? I also have a self.tableView.reloadData() in my viewDidAppear() method. Let me know.

override func viewDidLoad() {
    super.viewDidLoad()

    colorArray += [UIColor.whiteColor(), UIColor.blackColor(), UIColor.greenColor(), UIColor.yellowColor(), UIColor.redColor(), UIColor.purpleColor(), UIColor.orangeColor(), UIColor.blueColor(), UIColor.brownColor(), UIColor.darkGrayColor(), UIColor.lightGrayColor(), UIColor.grayColor(), UIColor.cyanColor(), UIColor.magentaColor(), UIColor.clearColor()]

} 

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    for color in colorArray {
        cell.backgroundColor = color
    }

    return cell
}
Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
Jamie Baker
  • 157
  • 6

5 Answers5

2

You are repeatedly setting the cells' colors.

Each time the table view wants to load a new cell, it calls its data sources' tableView(:cellForViewAtIndexPath:) method.

In your implementation of this method, you loop through the array and set the color of the cell to each item of the array. So first it will be white, then black, then green... and finally it is set to clearColor, which is transparent.

Every time that method gets called, your cells' colors will all be set to transparent. So sad!

By looking at your code, I think what you want is to set the first cell's color to white, the second to black, the third to green, etc.

You should know that each time tableView(:cellForRowAtIndexPath:) is called, it provides you with an indexPath argument that tells you which cell it wants. You just need to make use of that to implement your method:

// replace your for loop with this
cell.backgroundColor = colorArray[indexPath.row]

This way, when it wants the first cell, you set the cell to the first color. when it wants the second cell, you set the cell to the second color etc.

On the other hand, if you want to pick randomly from that colorArray so the cells will be different each time the table view is loaded, you just need to generate a random number and call the subscript of colorArray with that number:

// replace your for loop with this
let randomNumber = arc4random_uniform(UInt32(colorArray.count)) // line 1
cell.backgroundColor = colorArray[randomNumber] // line 2

line 1 generates a random number between 0 and colorArray.count. And line 2 just sets the cell's color to the color at that index of colorArray!

Straightforward, right?

EDIT: More about arc4random_uniform

You must be wondering what is this arc4random_uniform function doing here.

arc4random_uniform returns a random number (uniformly distributed) between 0 (inclusive) and the argument (exclusive). In other words, arc4random_uniform(5) will return 0, 1, 2, 3, 4 but not 5.

The function takes a parameter of type UInt32 so I cannot pass in colorArray.count directly.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

To Set Random Background of Cell. Working Code. Here arc4random_uniform() Take random Number 0 to colorArray.count.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    cell.backgroundColor = colorArray[arc4random_uniform(colorArray.count)]
    return cell
}
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
0

this will set everytime the last UIColor in cycle,

    for color in colorArray {
    cell.backgroundColor = color
}

So, you need just get it by indexPath.row (or section):

cell.backgroundColor = colorArray[indexPath.row]

Or get it randomly like this:

let randomInt = Int(rand()) % array.count
cell.backgroundColor = colorArray[randomInt]
Alex Kosyakov
  • 2,095
  • 1
  • 17
  • 25
0

try this code,

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        cell.backgroundColor = self.colors[indexPath.row % self.colors.count]

        return cell
    }

hope its helpful

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30
0

This will not work because the tableView(cellForRowAtIndexPath) is called once for every cell.

Now whats happening is that your cell.backgroundColor gets set to the fist color, then the second color immediately after that, etc. So the backgroundColor is always the color of the last element in your colorArray.

Use this instead of your for-in loop:

cell.backgroundColor = colorArray[indexPath.row]

or

If you have more cells in your tableView than colors in your array use this: (This will start at 0 again when over colorArray.count

override func viewDidLoad() {
    super.viewDidLoad()

    colorArray += [UIColor.whiteColor(), UIColor.blackColor(), UIColor.greenColor(), UIColor.yellowColor(), UIColor.redColor(), UIColor.purpleColor(), UIColor.orangeColor(), UIColor.blueColor(), UIColor.brownColor(), UIColor.darkGrayColor(), UIColor.lightGrayColor(), UIColor.grayColor(), UIColor.cyanColor(), UIColor.magentaColor(), UIColor.clearColor()]

} 

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

    let cycle = i / colorArray.count
    let colorIndex = i - (cycle * colorArray.count)

    cell.backgroundColor = colorArray[colorIndex]

    return cell
}
123FLO321
  • 854
  • 1
  • 7
  • 20