ios - How to resize a UITableview to fit the dynamic number of rows -
i'm building view controller elements of uiview , 1 uitableview. view larger iphone screen put elements in uiscrollview , configured auto layout.
the number of rows in uitableview dynamic, , need content visible in single screen. don't want enable uitableview scroll property because complete messy working master uiscrollview , uitableview scroll
i have been researching days on stackoverflow posts problem , couldn't find problem.
this code trying resize uitableview , uiscrollview
@iboutlet weak var scrollview: uiscrollview! @iboutlet weak var tableview: uitableview! override func viewdidload() { super.viewdidload() // configuring datasource delegate self.tableview.datasource = self self.tableview.delegate = self // sizing elements inside scrollview var contentrect = cgrectzero view:uiview in self.scrollview.subviews { contentrect = cgrectunion(contentrect, view.frame) } print(contentrect) // trying resize tableview , scrollview fit more contents self.tableview.contentsize = cgsizemake(scrollview.frame.width, cgfloat(441)) scrollview.contentsize = cgsizemake(scrollview.frame.width, cgfloat(877)) scrollview.delayscontenttouches = true scrollview.cancancelcontenttouches = false // looking new size , nothing change var contentrect2 = cgrectzero view:uiview in self.scrollview.subviews { contentrect2 = cgrectunion(contentrect2, view.frame) } print(contentrect2) print("tableview width \(self.tableview.frame.width)") print("tableview height \(self.tableview.frame.height)") }
the sample project on github
i appreciate help
layout of viewcontrol:
the short, wrong answer question add autolayout constraint tableview constraining height constant size. can set best guess, @ runtime set equal tableview's content size's height.
the problem approach eliminates benefit of tableview's , reusable cells. table views expressly designed minimize memory footprint of view. takes lot more memory store view (cell) represents data keep track data you're filling these cells in with.
so instead, better solution use multiple sections within table view. using example screenshot, parent scroll view tableview instead of scroll view. have 4 sections. in first section, return single row yellow view configured table view cell, , same in third & fourth sections. in second section, return many rows using in table view.
alternatively, yellow view thought table header, , blue & red views become table footer, rows in middle.
Comments
Post a Comment