选择 1 个集合视图中的单元格以更改第二个集合视图属性 (Xcode)

Selecting cell in 1 Collection View to change second Collection View properties (Xcode)

我在 Xcode 中设置了 2 个集合视图 - 一个包含带有背景颜色的空单元格,另一个包含图像集合。

当我 select 第一个集合视图中的一个单元格时,我想更改第二个集合视图中的所有背景颜色以匹配我 select 编辑的单元格的背景颜色。

我遇到的问题是,只有具有匹配索引路径的单元格在第二个集合视图中发生变化,即如果我 select 第 3 种颜色,第 3 个图像会发生变化。

如何才能让第二个 Collection View 中的所有单元格都改变颜色?我知道这与索引路径有关,但不知道如何绕过它。

代码:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout  {
    
    
    
    @IBOutlet weak var colourView: UICollectionView!
    
    
    @IBOutlet weak var iconView: UICollectionView!
    
    
    
    @IBOutlet weak var height: NSLayoutConstraint!
    
   
    
    let colours = [
            UIColor(red: 0.94, green: 0.31, blue: 0.41, alpha: 1.00),
            UIColor(red: 0.95, green: 0.45, blue: 0.23, alpha: 1.00),
            UIColor(red: 1.00, green: 0.89, blue: 0.49, alpha: 1.00),
            UIColor(red: 0.73, green: 0.72, blue: 0.42, alpha: 1.00),
            UIColor(red: 0.01, green: 0.44, blue: 0.61, alpha: 1.00),
            UIColor(red: 0.56, green: 0.89, blue: 0.90, alpha: 1.00),
            UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 1.00),
   
  ]
       
       
    
       
    let images = [UIImage(named: "home"), UIImage(named: "work"), UIImage(named: "key"), UIImage(named: "supermarket"), UIImage(named: "restaurant"), UIImage(named: "tree") , UIImage(named: "parking"), UIImage(named: "rugby"), UIImage(named: "medicine"), UIImage(named: "mortar"), UIImage(named: "ticket"), UIImage(named: "star")]
    
    

      
       
       override func viewDidLoad() {
           super.viewDidLoad()
        
        
   
        
         
           // Do any additional setup after loading the view, typically from a nib.
       }
       
     func numberOfSections(in collectionView: UICollectionView) -> Int {
        1
    }
    
       func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
           if (collectionView == iconView){
            return self.images.count
           }
           
           
        return self.colours.count
       }
    
   
       
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
       
    let width = view.frame.size.width
        
        if (collectionView == iconView){
           return CGSize(width: width * 0.2, height: width * 0.2)
        
            
        }
                
    return CGSize(width: width * 0.1, height: width * 0.1)
       
     
              }

    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        if (collectionView == iconView){
                     let cell2 = iconView.dequeueReusableCell(withReuseIdentifier: "iconcell", for: indexPath) as! IconCollectionViewCell
                     
                     cell2.iconimage.image = self.images[indexPath.item]
             
                     return cell2
            
           
            
                 }
            let colourcell = colourView.dequeueReusableCell(withReuseIdentifier: "colourcell", for: indexPath) as! ColourCollectionViewCell
           colourcell.backgroundColor = colours[indexPath.row]
           
           
        
           
           return colourcell
       }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
                       let cell = colourView.cellForItem(at: indexPath) as! ColourCollectionViewCell
        
                        let cell2 = iconView.cellForItem(at: indexPath) as! IconCollectionViewCell
        
   

                       if cell.isSelected {
                        cell2.iconimage.backgroundColor = cell.backgroundColor
                       }


           
}
}

提前致谢

声明一个变量:

class ViewController:
  var newColor : UIColor?

改变这个:

if cell.isSelected {
   cell2.iconimage.backgroundColor = cell.backgroundColor
}

为此:

 if cell.isSelected {
    newColor = cell.backgroundColor
    iconView.reloadData()
 }

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

放这个

if newColor != nil { // maybe a better test here, if cell was selected
   cell2.iconimage.backgroundColor = newColor
}

之前

 return cell2