uitableview - Swift 3 - Custom TableViewCell dynamic height - programatically -
i trying setup dynamic height custom table view cell...programatically.
there many tutorials doing inside storyboard, pure programmatic solution:
class customtableviewcell: uitableviewcell { var container: uiview = { let view = uiview() view.translatesautoresizingmaskintoconstraints = false return view }() override func awakefromnib() { super.awakefromnib() self.setupview() self.addconstraints() } func setupview() { self.contentview.addsubview(self.container) self.container.backgroundcolor = uicolor.cyan } func addconstraints() { self.container.leadinganchor.constraint(equalto: self.contentview.leadinganchor, constant: 0).isactive = true self.container.trailinganchor.constraint(equalto: self.contentview.trailinganchor, constant: 0).isactive = true self.container.topanchor.constraint(equalto: self.contentview.topanchor, constant: 0).isactive = true self.container.bottomanchor.constraint(equalto: self.contentview.bottomanchor, constant: 0).isactive = true self.container.heightanchor.constraint(equaltoconstant: 200) } override func setselected(_ selected: bool, animated: bool) { super.setselected(selected, animated: animated) } }
and view registering it:
class customviewcontroller: uiviewcontroller { var data: [post] = [] let tableview: uitableview = { let tableview = uitableview() tableview.translatesautoresizingmaskintoconstraints = false return tableview }() override func viewdidload() { super.viewdidload() self.setupview() self.addconstraints() self.getdata() } override func didreceivememorywarning() { super.didreceivememorywarning() } func setupview() { self.view.addsubview(self.tableview) self.tableview.delegate = self self.tableview.datasource = self self.tableview.register(uinib(nibname: "customtableviewcell", bundle: nil), forcellreuseidentifier: "cellcustom") self.tableview.estimatedrowheight = 100 self.tableview.rowheight = uitableviewautomaticdimension } func addconstraints() { self.tableview.leadinganchor.constraint(equalto: self.view.leadinganchor, constant: 40).isactive = true self.tableview.trailinganchor.constraint(equalto: self.view.trailinganchor, constant: -40).isactive = true self.tableview.topanchor.constraint(equalto: self.view.topanchor, constant: 40).isactive = true self.tableview.bottomanchor.constraint(equalto: self.view.bottomanchor, constant: -40).isactive = true } func getdata() { // networking stuff } } extension customviewcontroller: uitableviewdelegate { } extension streamviewcontroller: uitableviewdatasource { func numberofsections(in tableview: uitableview) -> int { return 1 } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { return data.count } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "cellcustom", for: indexpath) as! customtableviewcell return cell } }
i trying resize custom tableview cell bases on height constraint:
self.container.heightanchor.constraint(equaltoconstant: 200)
but not work.. custom tableview cell stays @ "44"..
anybody tell me wrong pure programmtic solution?
thanks , greetings
the problem seems height constraint not active
func addconstraints() { ... self.container.heightanchor.constraint(equaltoconstant: 200) }
thats why uitableviewautomaticdimension
not work.
change self.container.heightanchor.constraint(equaltoconstant: 200).isactive = true
, should work
Comments
Post a Comment