ios - Passing data to custom UITableViewCell with adding subviews -


i'm passing data custom uitableviewcell , based on data want show or hide dynamically added subview. i'm reusing cell created in storyboard. working expected, until of cells reused, example while scrolling "randomly" hide or show dynamic added subview.

what need

i need way set data of cell through method (setdata), adding dynamically created subview, while allowing cell reused without creating glitches in appearance, in particular added subview cells state.

problem

i don't know should create subview, doesn't have recreated when cell reused , won't bug when want hide or show in setdata method. having access iboutlet storyboardlabel while creating new subview.

customtableviewcell

class customtableviewcell : uitableviewcell {    var data: dataitem?    var customsubview: uiview?     @iboutlet weak var storyboardlabel: uilabel!     //setting data of cell , adding subview    func setdata(dataitem data) {       // adding view       let customsubview = uiview.init(...)       customsubview.bounds = storyboardlabel.bounds       customsubview.hidden = data.showsubview       self.contentview.addsubview(customsubview)    } } 

adding cell

override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {     let cell = tableview.dequeuereusablecellwithidentifier("storyboardtablecell") as! customtableviewcell     cell.setdata(self.data[indexpath.section][indexpath.row] as! dataitem)     return cell } 

everything working expected, until of cells reused...

nothing working expected: adding object cached uitableviewcell, , of course, when passed cached cell again on subsequent cellforrowatindexpath, adding new objects.

consider using collection of views in ib, each satisfying cell organization, saving adding subviews programmatically.

you need clean cell.

  • either remove have added in cellforrowatindexpath
    • you can use tags , viewwithtag if want refrain re-adding existing views
  • or implement prepareforreuse()

you can find example (and similar discussion) on this stack overflow post.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -