Swift: UICollectionViewCell didSelectItemAtIndexPath Change backgroundColor

You can use this method for that:

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){

    var cell : UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!
    cell.backgroundColor = UIColor.magentaColor()
}

Old question, but it can help someone:

You can't simply modify the cell, since your changes will be lost when you scroll your UICollectionView, or even worst, other cells could appear with a wrong background, because they'll be reused.

So, the best way to do that is create an array of NSIndexPath and append your selected indexPaths:

var selectedIndexes = [NSIndexPath]() {
    didSet {
        collectionView.reloadData()
    }
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){
    // ...
    if let indexSelecionado = selectedIndexes.indexOf(indexPath) {
        selectedIndexes.removeAtIndex(indexSelecionado)
    } else {
        selectedIndexes.append(indexPath)
    }
}

// ...

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    // ...
    if self.selectedIndexes.indexOf(indexPath) == nil {
        cell.backgroundColor = UIColor.whiteColor() // Unselected
    } else {
        cell.backgroundColor = UIColor.redColor() // Selected
    }
    return cell
}

You can override UICollectionViewCell isSelected . It will apply changes in selected method.

class ButtonCollectionCell: UICollectionViewCell {

            override var isSelected: Bool{
                didSet{
                    if self.isSelected
                    {
                       self.layer.backgroundColor =  colorLiteral(red: 0.8058760762, green: 0.2736578584, blue: 0.1300437152, alpha: 1)

                    }  else
                    {
                       self.layer.backgroundColor =  colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)

                    }
                }
            }

    }