ios - Populate a Prototype Cell by Passing Object -


i have uitableview using prototype cells (everything in storyboard). ideally, pass these cells object via cellforrowatindexpath , have cell figure out how populate it's labels etc...

setselected seems work, if cells not scrolled off screen. once scrolled off screen of cell's logic broken. see sample code current approach:

class requirementsandcontentscell: uitableviewcell {      var represents = string()     var item = plitem()     var requirement = plcaserequirement()     var content = plcasecontent()      @iboutlet weak var itemnamelabel: uilabel!     @iboutlet weak var tagview: uiview!     @iboutlet weak var taglabel: uilabel!     @iboutlet weak var barcodeicon: uiimageview!     @iboutlet weak var barcodelabel: uilabel!     @iboutlet weak var unitcountlabel: uilabel!     @iboutlet weak var statusview: uiview!     @iboutlet weak var mainlabelverticalalign: nslayoutconstraint!     @iboutlet weak var barcodeiconleadingspace: nslayoutconstraint!     @iboutlet weak var tagviewleadingspace: nslayoutconstraint!       override func setselected(selected: bool, animated: bool) {         super.setselected(selected, animated: animated)          tagview.layer.cornerradius = 5          if represents == "content" {             itemnamelabel.text = content.name             unitcountlabel.attributedtext = createattributedstring("\(content.qty)", "\nunits", "helveticaneue-light", 12, uicolor.darktextcolor())             barcodelabel.text = content.barcode             statusview.hidden = true             if content.isparent == true {                 tagview.hidden = true                 tagviewleadingspace.priority = 1             }         } else if represents == "requirement" {             itemnamelabel.text = requirement.name             mainlabelverticalalign.constant = 0             tagview.hidden = true             barcodeicon.hidden = true             barcodelabel.hidden = true             unitcountlabel.attributedtext = createattributedstring("\(requirement.qty)", "\nunits", "helveticaneue-light", 12, uicolor.darktextcolor())             if item.iscontainer == true {                 updatestatusview()             } else {                 statusview.hidden = true             }          }       }        func updatestatusview () {         if requirement.fulfillmentlevel == 0 {             statusview.layer.backgroundcolor = uicolor.redcolor().cgcolor         } else if requirement.fulfillmentlevel == 1 {             statusview.layer.backgroundcolor = globalvars.yellowcolor.cgcolor         } else if requirement.fulfillmentlevel == 2 {             statusview.layer.backgroundcolor = globalvars.goodcolor.cgcolor         } else {             statusview.layer.backgroundcolor = uicolor.orangecolor().cgcolor         }     }  } 

what proper way of achieving desired result?

you shouldn't trying populate cell in setselected. create method takes object you're tying pass argument, , call cellforrowatindexpath.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -