0

我正在嘗試有一個collectionView,用戶可以選擇多個單元格,並讓他們突出顯示/取消選擇它們。我似乎無法弄清楚如何在點擊時更改單元格的背景顏色。UICollectionViewCell不改變顏色選擇

這裏是我建立的CollectionView:

let extraCollectionView = UIView() 
let flowLayout = UICollectionViewFlowLayout() 

     extraCollectionView.frame = CGRect(x:self.view.frame.size.width * 1.5, y:self.view.frame.size.height/6.5, width: self.view.frame.width/1.3, height: self.view.frame.height/1.35) 


     extraCollectionView.backgroundColor = UIColor.clear 
     self.view.addSubview(extraCollectionView) 


     let collectionView = UICollectionView(frame: CGRect(x: extraCollectionView.frame.width * 0, y:extraCollectionView.frame.height * 0, width: extraCollectionView.frame.width, height: extraCollectionView.frame.height), collectionViewLayout: flowLayout) 


     collectionView.register(uploadGenreSelectionCVC.self, forCellWithReuseIdentifier: cellId) 
     collectionView.delegate = self 
     collectionView.dataSource = self 

     collectionView.layer.cornerRadius = collectionView.layer.frame.width/10 
     collectionView.backgroundColor = UIColor.clear 
     collectionView.layer.borderColor = UIColor.blue.cgColor 
     collectionView.layer.borderWidth = 3 
     collectionView.allowsMultipleSelection = true 
     extraCollectionView.addSubview(collectionView) 

這裏是我的CollectionView代碼:

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



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

     cell.layer.borderColor = UIColor.blue.cgColor 
     cell.layer.borderWidth = 2 
     cell.backgroundColor = UIColor.clear 


     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

     let width = (collectionView.bounds.width)/3 
     let height = (collectionView.bounds.height)/5 

     return CGSize(width:width, height:height) 
    } 


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 
     return 0 
    } 

    func collectionView(_ collectionView: UICollectionView, 
         layout collectionViewLayout: UICollectionViewLayout, 
         minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
     return 0 
    } 




    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! uploadGenreSelectionCVC 

     print("yo") 
     cell.contentView.layer.backgroundColor = UIColor.red.cgColor 
    } 

& &這裏是細胞本身

let genreLabel = UILabel() 

    override func draw(_ rect: CGRect) { 

     genreLabel.frame = CGRect(x: self.frame.size.width * 0, y: self.frame.size.height/2.7, width: self.frame.size.width, height: self.frame.size.height/3) 
     genreLabel.text = "Rap" 
     genreLabel.textColor = UIColor.white 
     genreLabel.textAlignment = .center 


     addSubview(genreLabel) 
    } 

什麼時我忘了做?

回答

0

覆蓋isSelected在你的細胞,並自定義單元格中給出的值,

override var isSelected: Bool { 
    didSet { 
     self.backgroundColor = isHighlighted ? .lightGray : .clear 
    } 
} 

注:此屬性是由collectionView

+0

自動設置我把我的CollectionViewCell類中,但它仍然是不加工..? – AndrewS

+0

刪除'didSelectItemAt'的實現,並且不要在'cellForItem'中設置'backgroundColor'。看看它是如何工作的。 –