rasche

1

Ich versuche, den Hintergrund der ersten beiden Zellen in meiner Sammlung Ansicht zu ändern habe ich diesen Code versuchtrasche

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 
} 

Es ändert sich die Farbe der ersten beiden Zellen die groß ist . Wenn ich jedoch in den Querformat- oder Bildlaufmodus umschalte, ändert sich der Hintergrund verschiedener Zellen, nicht nur der ersten beiden.

Frage: Wie kann ich den Hintergrund nur der ersten zwei Zellen ändern, nicht mater, was der Benutzer tut?

Antwort

1

Da Ihre Zelle wiederverwendet wird, müssen Sie die Standardfarbe für andere Zellen angeben.

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

danke arbeitete wie ein Charme. – c3pNoah

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