-2

I have a UICollectionView which displays content received from the web. The number of cells can be known only at runtime. Client's requirement is that each row in the collection view must have a different color.

For example, If I have to show 12 items in my UICollectionView then suppose if first row has 3 cells then the colour of all three cells should be red.

Cells of second row should be white and so on.

Could you please help me how to achieve it?

Thanks in advance.

Ashutosh Shukla
  • 358
  • 5
  • 14
  • Your title is very vague. You should change it properly reflect your question. Also, show your attempt at solving the problem. – Rakesha Shastri Dec 01 '18 at 10:19
  • Can you show what you tried so far? Also what's the issue you are facing? – Natarajan Dec 01 '18 at 10:19
  • Suppose I got number of items to be displayed are 12 from the web. I need to show them in a collection view and cells which are in same row should have same colour. For example first row has three cells then the colour of all should be red, I have followed this answer from SO https://stackoverflow.com/a/45307538/9585748 to solve my issue but could not solve it yet. – Ashutosh Shukla Dec 01 '18 at 10:47

2 Answers2

1

on cellForRowAt, if you want to intercalate colors, you can check indexPath.row and update the background color.

if indexPath.item % 2 == 0 {
   <#YourCell#>.backgroundColor = .blue
} else {
   <#YourCell#>.backgroundColor = .green
}

Also, if you want random colors, you can check this answer: How to make a random color with Swift Then you just set <#YourCell#>.backgroundColor the random color

In the case that the collectionViewCell has another collectionView on each cell, and those have to match the same color for each parent collectionView row, I'd suggest you to create a custom class for the parent collectionView cell, so that every collectionView child row can consume from it and set it's own backgroundColor.

Gustavo Vollbrecht
  • 3,188
  • 2
  • 19
  • 37
  • I want to assign a unique colour to each row i.e. it should be same for all the cells in that particular row. Your answer will not be helpful in that case. – Ashutosh Shukla Dec 01 '18 at 10:47
  • if you want to hold the same color for cells when scrolling, you can create an [UIColor] with size based on number of cells, and consume from it. Then, for each cell on a row (that is new information), consume from that array. – Gustavo Vollbrecht Dec 01 '18 at 10:49
  • Sorry for not framing question properly. I have updated question. – Ashutosh Shukla Dec 01 '18 at 10:53
  • Don't change background of the cell itself but it's `contentView`. `CollectionView` should use `indexPath.item`, not `row`. – Sulthan Dec 01 '18 at 10:54
0

Updated answer:

I'm supposing you have initially you have these variables like:

private let cellBackgroundColors: [UIColor] = [.yellow, .green, .blue, .purple]
private let numberOfRows = 3 (or you can assign it dynamically)

Second step, implement like this:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! ToDoListCollectionCell

    let index = indexPath.item / numberOfRows
    if index < cellBackgroundColors.count {
        cell.backgroundColor = cellBackgroundColors[index]
    } else {
        cell.backgroundColor = .white // Set your default color to handle this case
    }
    return cell
}

Happy Coding !!

nitin.agam
  • 1,949
  • 1
  • 17
  • 24