1

I am trying to change the background of the first two cells in my collection view i have tried this code

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = self.view.frame.width        
    let height = self.view.frame.height
    return CGSize(width: width / 2.2 , height: height / 6)
}

override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {   
    MyCollectionView.reloadData()
}

@IBAction func Back(_ sender: Any) {
    performSegue(withIdentifier: "fourtothree", sender: nil)
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {   
    return ScoreArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {    
    let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
    let MyCell = Cell.viewWithTag(1) as! UILabel
    MyCell.text = ScoreArray[indexPath.row]
    if indexPath.row == 0 {
        Cell.backgroundColor = UIColor.gray
        MyCell.font = UIFont.boldSystemFont(ofSize: 16.0)
    }
    if indexPath.row == 1 {   
        Cell.backgroundColor = UIColor.gray
        MyCell.font = UIFont.boldSystemFont(ofSize: 16.0)            
    }
    return Cell
}

It changes the color of the first two cells which is great. however when I rotate to landscape or scroll it changes the background of different cells not just the first two.

Question: How can I change the background of only the first two cells no mater what the user does?

c3pNoah
  • 65
  • 1
  • 12

2 Answers2

1

Since your cell get reused, you need to provide the default color for other cell.

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

    let MyCell = Cell.viewWithTag(1) as! UILabel
    MyCell.text = ScoreArray[indexPath.row]

    if indexPath.row == 0 || indexPath.row == 1 {
        Cell.backgroundColor = UIColor.gray
        MyCell.font = UIFont.boldSystemFont(ofSize: 16.0)
    } else {
        Cell.backgroundColor = UIColor.white //change with your default color
    }
    return Cell
}
seto nugroho
  • 1,359
  • 1
  • 13
  • 20
0
you can change the color of ay cell but as in cellForItemAt indexPath  function you are using 
    let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell that statement reuse the cell to reduce the memory usage , So to overcome this problem use 

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {    
        let Cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
        let MyCell = Cell.viewWithTag(1) as! UILabel
        MyCell.text = ScoreArray[indexPath.row]
        MyCell.font = UIFont.boldSystemFont(ofSize: 16.0)
        if indexPath.row == 0 || indexPath.row == 1 {
            Cell.backgroundColor = UIColor.gray

        }
        else   {   
            Cell.backgroundColor = UIColor.clear
        }
        return Cell
    }
Optimus
  • 800
  • 5
  • 16