1

I am attempting to have a collectionView where users are able to select multiple cells, and have them highlight/deselect them. I can't seem to figure out how to even just change the background colors of the cells when they are tapped.

Here is where I am setting up the 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)

Here is my collectionView code:

 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
    }

&& Here is the cell itself

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

What am I forgetting to do??

AndrewS
  • 351
  • 4
  • 18

1 Answers1

2

Override isSelected in your cell, and customize your cell given the value,

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

Note: this property is set automatically by collectionView

Jože Ws
  • 1,754
  • 17
  • 12
  • I put that within my CollectionViewCell class, but it is still not working..? – AndrewS May 27 '17 at 18:33
  • remove your implementation of `didSelectItemAt` and do not set `backgroundColor` in `cellForItem`. See how that works. – Jože Ws May 27 '17 at 18:35