ios - UICollectionviewCell backgroundColor not Change when selected -
i'm trying change backgorund color of cell when selected. cell background color not changing.
func collectionview(_ collectionview: uicollectionview, didselectitemat indexpath: indexpath) { let cell = collectionview.dequeuereusablecell(withreuseidentifier: "cell", for: indexpath) as! categorycollectionviewcell let category = self.categories[indexpath.row] switch cell.isselected { case true: cell.backgroundcolor = .black default: cell.backgroundcolor = .white } cell.setneedsdisplay() }
you don't need manually change background color upon selection. uicollectionviewcell has property called selectedbackgroundview
precisely purpose.
use in collectionview(_:cellforitemat:)
delegate method follows:
func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) { let cell = collectionview.dequeuereusablecell(withreuseidentifier: "cell", for: indexpath) as! categorycollectionviewcell cell.selectedbackgroundview = uiview(frame: cell.bounds) cell.selectedbackgroundview!.backgroundcolor = .black return cell }
Comments
Post a Comment