ios - Programmatically added UITableView won't populate data -
i wish create uitableview
, custom cell, , add them uiviewcontroller
.
if layout uitableview
storyboard onto uiviewcontroller
, everything works fine custom cell.
but realized animation regarding height not smooth - table view disappears
so decided create/destroy uitableview
everytime wish animation, , add subview uiviewcontroller
.
but programmatically-added uitableview
won't populate data.
what doing wrong here?
import uikit class viewcontroller: uiviewcontroller, uitableviewdelegate, uitableviewdatasource { var dataarr = [string]() // holds data uitableview var tv: uitableview! // notice it's not @outlet override func viewdidload() { super.viewdidload() dataarr = ["one", "two", "three"] // create , add uitableview drop down tv = uitableview() tv.delegate = self tv.datasource = self createtableview() } func createtableview() { let w = uiscreen.mainscreen().bounds.size.width tv.frame = cgrectmake(0, 50, w, 0) tv.rowheight = 25.0 // register custom cell var nib = uinib(nibname: "searchcell", bundle: nil) tv.registernib(nib, forcellreuseidentifier: "searchcell") self.view.addsubview(tv) uiview.animatewithduration(0.2, delay: 0.0, options: uiviewanimationoptions.beginfromcurrentstate, animations: { self.tv.frame.size.height = 100 }, completion: { (didfinish) in self.tv.reloaddata() }) } func destroytableview() { uiview.animatewithduration(0.2, animations: { // hide self.tv.frame.size.height = 0 }, completion: { (didfinish) in self.tv.removefromsuperview() }) } // mark: - uitableview func numberofsectionsintableview(tableview: uitableview) -> int { return 1 } func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return dataarr.count } func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell: searchcell = self.tv.dequeuereusablecellwithidentifier("searchcell") as! searchcell cell.celllabel.text = dataarr[indexpath.row] return cell; } }
i think problem line.
self.tv.frame.size.height = 100
try setting whole frame instead of height. pretty sure uiview's frame's properties read only.
see here
if doesn't work. maybe try implementing
func tableview(_ tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat
Comments
Post a Comment