ios - CollectionView objects (Swift) -
i want highlight change size , appearance of object inside collection view.
how can set object properties in collection view cell, within "didhighlight" method?
in "cellforitematindexpath" declare reusable cells class
and use "cell.myoutlet.backgroundcolor = uicolor.bluecolor()"
func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { if collectionview == self.collectionviewcontroller { let (friendfirstname,friendlastname) = friends[indexpath.row] let cell: customcella = collectionview.dequeuereusablecellwithreuseidentifier("democell", forindexpath: indexpath) as! customcella if indexpath.section == 0 { cell.celltitle.text = name cell.imgcell.image = uiimage(named: pics[indexpath.row]) cell.imgcell.layer.maskstobounds = true cell.self.imgcell.layer.cornerradius = 20 return cell } else { let cell2: addcell = collectionview.dequeuereusablecellwithreuseidentifier("democell2", forindexpath: indexpath) as! addcell return cell2 } } else if collectionview == self.emojicollectionviewcontroller { let cellb: customcellb = collectionview.dequeuereusablecellwithreuseidentifier("democellb", forindexpath: indexpath) as! customcellb cellb.mylabel.text = arrayone[indexpath.row] return cellb } else { let cellc: customcellc = collectionview.dequeuereusablecellwithreuseidentifier("democellc", forindexpath: indexpath) as! customcellc // ...set cell let height = self.collectionviewcontroller2.frame.height cellc.frame = cgrectmake(cellb.frame.origin.x, 0, cellb.frame.size.width, height) cellc.updateconstraintsifneeded() cellc.layoutifneeded() cellc.imgvw.image = uiimage(named: pictures[indexpath.row] as! string) return cellc } }
func collectionview(collectionview: uicollectionview, didhighlightitematindexpath indexpath: nsindexpath) { if collectionview == self.collectionviewcontroller { if indexpath.section == 0 { let cell: customcella = customcellb() cell.mylabel.backgroundcolor = uicolor.bluecolor() //crashes due nil value) } } else { } }
i tried using similar definition in didhighlight , keeps crashing.
let didhighlightitematindexpath
change data, not view. so, make friends[indexpath.row]
object or add parameter tuple. , in didhighlightitematindexpath
following:
if collectionview == self.collectionviewcontroller { if indexpath.section == 0 { let (fname, lname, color) = friends[indexpath.row]; friends[indexpath.row] = (fname, lname, uicolor.bluecolor()) } }
and in cellforitematindexpath
:
if collectionview == self.collectionviewcontroller { let (friendfirstname, friendlastname, color) = friends[indexpath.row] if indexpath.section != 0 { let cell = collectionview.dequeuereusablecellwithreuseidentifier("democell2", forindexpath: indexpath) as! addcell; return cell; } else if color == nil { let cell = collectionview.dequeuereusablecellwithreuseidentifier("democell", forindexpath: indexpath) as! customcella; cell.celltitle.text = name cell.imgcell.image = uiimage(named: pics[indexpath.row]) cell.imgcell.layer.maskstobounds = true cell.self.imgcell.layer.cornerradius = 20 return cell } else { cell = collectionview.dequeuereusablecellwithreuseidentifier("democellb", forindexpath: indexpath) as! customcellb; // code customcellb return cell; } }
edit: updated, instead of objects uses tuples. added functionality need. basically, need create 2 prototype cells in interface builder different reuse identifiers , classes. , dequeue correct identifier in index path. also, refactored of code , if create different function each collectionview , like:
if collectionview == self.collectionviewcontroller { return self.dequeuecollectioncell(indexpath); } else if collectionview == self.emojicollectionviewcontroller { return self.dequeuemojicell(indexpath); } else { return self.dequeuesomeothercell(indexpath); }
also, code provided... hope not actual production code , changed values forum. otherwise, in couple of days even, going lost in happening here. many inconsistent variable names , identifiers.
one more also. use naming conventions in class names. read forum post more information. apple uses camelcase everywhere. in majority of instances, first letter capitalized class names, not object names.
Comments
Post a Comment