ios - UICollectionview Reuse Existing cell -


i want generate collectionview 100 number of cells, should not reallocate @ every time of scrolling, in code creating cells newly.

any 1 me avoid issue, please find code below,

func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int {     return colorarray.count         }  func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell {      let collecell: colorcell = collectionview.dequeuereusablecellwithreuseidentifier("cell", forindexpath: indexpath) as! colorcell      collecell.bgcolor.backgroundcolor = uicolor(red: colorarray[indexpath.row].valueforkey("red") as! cgfloat/255, green: colorarray[indexpath.row].valueforkey("green") as! cgfloat/255, blue: colorarray[indexpath.row].valueforkey("blue") as! cgfloat/255, alpha: 1.0)      return collecell }  func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) {     let cell:colorcell = collectionview.cellforitematindexpath(indexpath) as! colorcell      cell.layer.borderwidth = 2.0     cell.layer.bordercolor = uicolor.whitecolor().cgcolor      selectedcolor = indexpath.row      samplecolorview.backgroundcolor = uicolor(red: colorarray[indexpath.row].valueforkey("red") as! cgfloat/255, green: colorarray[indexpath.row].valueforkey("green") as! cgfloat/255, blue: colorarray[indexpath.row].valueforkey("blue") as! cgfloat/255, alpha: 1.0)      self.view.backgroundcolor = uicolor(red: colorarray[indexpath.row].valueforkey("red") as! cgfloat/255, green: colorarray[indexpath.row].valueforkey("green") as! cgfloat/255, blue: colorarray[indexpath.row].valueforkey("blue") as! cgfloat/255, alpha: 0.7)      samplecolorview.layer.cornerradius = 75 }    func collectionview(collectionview: uicollectionview, diddeselectitematindexpath indexpath: nsindexpath) {     let cell:colorcell = collectionview.cellforitematindexpath(indexpath) as! colorcell      selectedindex = -1;      cell.layer.borderwidth = 0.0     cell.layer.bordercolor = uicolor.graycolor().cgcolor      self.view.backgroundcolor = uicolor.graycolor()  } 

i think it's reused correctly, plz double check? suggest way set selected/deselected cell's style, can override selected property of uicollectionviewcell.

class colorcell : uicollectionviewcell{     override var selected: bool{         didset {             layer.borderwidth = selected ? 2 : 0             layer.bordercolor = selected ? uicolor.whitecolor().cgcolor : uicolor.graycolor().cgcolor         }     /* code */ } 

and uicollectionviewdelegate implementation:

func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) {    selectedcolor = indexpath.row    samplecolorview.backgroundcolor = /*color*/    view.backgroundcolor = /*color*/    samplecolorview.layer.cornerradius = 75 }  func collectionview(collectionview: uicollectionview, diddeselectitematindexpath indexpath: nsindexpath) {     selectedindex = -1;     self.view.backgroundcolor = uicolor.graycolor() } 

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -