ios - addSubview on cellForItemAtIndexPath issue in Swift -
i have collectionview custom cells. trying add dynamic labels in each cell, added last cell shown. if make scroll, labels disappear of cell , appear in new cell shown. number , text of labels achieved web service. cellforitematindexpath function:
override func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { let cell = collectionview.dequeuereusablecellwithreuseidentifier(reuseidentifier, forindexpath: indexpath) as! undatedcell cell.titlelabel.text = "sacar al perro" cell.locationlabel.text = "casa" var aux: cgfloat = 0 label in labelsarray { let labelwidht = calculatelabelwidht(label) + 20 cell.labelsview.addsubview(label) label.leftanchor.constraintequaltoanchor(cell.labelsview.leftanchor, constant: aux).active = true label.centeryanchor.constraintequaltoanchor(cell.labelsview.centeryanchor).active = true label.widthanchor.constraintequaltoconstant(labelwidht).active = true aux = aux + labelwidht + 8 } return cell }
in viewdidload create demo labels:
override func viewdidload() { super.viewdidload() self.collectionview!.registerclass(undatedcell.self, forcellwithreuseidentifier: reuseidentifier) self.collectionview?.alwaysbouncevertical = true let label1 = labellabel() label1.text = "zara" labelsarray.append(label1) let label2 = labellabel() label2.text = "perro" labelsarray.append(label2) let label3 = labellabel() label3.text = "amigo" labelsarray.append(label3) }
finally, pic of result:
thank time!
Comments
Post a Comment