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

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -