ios - How to add programmatic constraints to a UITableView? -
i have class set follows:
class settingscontroller: uitableviewcontroller {
i able add constraints uitableview there equal spacing either side of table (it full width table, little ugly on ipad).
i have read can't add constraints uitableviewcontroller
, , instead must add uiviewcontroller
class, add uitableview
that, , should able add constraints tableview.
i've amended class , defined uitableview
on next line. here first 2 lines now:
class settingscontroller: uiviewcontroller { var tableview = uitableview()
further down, have code attach tableview uiview
:
override func viewdidload() { super.viewdidload() self.view.addsubview(tableview) }
the app builds when trying access view, screen blank, i've not been able attempt constraints part yet (the screen display data correctly if revert i've described).
many of existing functions within class reference tableview
passing in data/setting number of rows etc, seems i've not done right... there piece i'm missing?
here simple example of programmatically creating , adding table view "normal" view:
// // settingscontroller.swift // // created don mag on 7/25/17. // import uikit class settingscontroller : uiviewcontroller, uitableviewdelegate, uitableviewdatasource { let tableview : uitableview = { let t = uitableview() t.translatesautoresizingmaskintoconstraints = false return t }() override func viewdidload() { super.viewdidload() // set background color can see table self.view.backgroundcolor = uicolor.blue // add table view self.view self.view.addsubview(tableview) // constrain table view 120-pts on top, // 32-pts on left, right , bottom (just demonstrate size/position) tableview.leftanchor.constraint(equalto: view.leftanchor, constant: 32.0).isactive = true tableview.topanchor.constraint(equalto: view.topanchor, constant: 120.0).isactive = true tableview.rightanchor.constraint(equalto: view.rightanchor, constant: -32.0).isactive = true tableview.bottomanchor.constraint(equalto: view.bottomanchor, constant: -32.0).isactive = true // set delegate , datasource tableview.delegate = self tableview.datasource = self // register defalut cell tableview.register(uitableviewcell.self, forcellreuseidentifier: "cell") } // note: because not subclassed uitableviewcontroller, // datasource , delegate functions not overridden // mark: - table view data source func numberofsections(in tableview: uitableview) -> int { return 1 } func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int { return 25 } func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath) cell.textlabel?.text = "\(indexpath)" return cell } // mark: - table view delegate func tableview(_ tableview: uitableview, didselectrowat indexpath: indexpath) { // etc } }
you should able run without edits, , result along these lines: simple example, should pretty easy see what's going on.
if have (i'm assuming, complex) settings uitableviewcontroller
class working, should pretty straight-forward follow skeleton , convert yours uiviewcontroller
+ uitableview-as-subview
.
alternatively, can load existing uitableviewcontroller
child view controller, , add view (the tableview) subview.
Comments
Post a Comment