0

ユーザーが複数のセルを選択して強調表示/選択解除できるコレクションビューを作成しようとしています。私は彼らがタップされているときだけ、細胞の背景色を変更する方法を把握していないようです。私はcollectionviewを設定していますのはここUICollectionViewCellが選択時に色を変更しない

は次のとおりです。ここで

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'を設定しないでください。その仕組みを見てください。 –

関連する問題