更改特定 UICollectionView 单元格的背景 swift

Change background of specific UICollectionView cells swift

我正在尝试更改集合视图中前两个单元格的背景我已经尝试过此代码

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
}

它改变了前两个单元格的颜色,这很棒。然而,当我旋转到横向或滚动时,它会改变不同单元格的背景,而不仅仅是前两个。

问题:无论用户做什么,如何只更改前两个单元格的背景?

由于您的单元格被重复使用,您需要为其他单元格提供默认颜色。

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