Change Background of UICollectionView Cell on Tap

后端 未结 6 902
时光说笑
时光说笑 2020-12-08 04:13

I have a UICollectionView that I have created programmatically. I would like for the collection view to behave in the following way:

1. User touches cell
2.          


        
6条回答
  •  独厮守ぢ
    2020-12-08 04:58

    Simple binary logic solution. Works with Swift 3 and 4:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt
        indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as! CategoryCell
        let lastCellColor = cell.backgroundColor
        if cell.isSelected {cell.backgroundColor = .green} else {cell.backgroundColor = lastCellColor}
    }
    

提交回复
热议问题