Reputation: 31
I'm building a View Controller with some elements of UIView and one UITableView. This view will be larger than an iPhone screen than I put all those elements in a UIScrollView and configured auto layout.
The number of rows in a UITableView are dynamic, and I need that all content be visible in a single screen. I don't want to enable UITableView scroll property because will be complete messy working with master UIScrollView and the UITableview scroll
I have been researching for days on StackOverflow for posts about this problem and couldn't find anything like my problem.
This is the code trying to resize UITableView and UIScrollView
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Configuring dataSource an delegate
self.tableView.dataSource = self
self.tableView.delegate = self
// Sizing all the elements inside scrollView
var contentRect = CGRectZero
for view:UIView in self.scrollView.subviews {
contentRect = CGRectUnion(contentRect, view.frame)
}
print(contentRect)
// Trying to resize the tableView and scrollView to 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 for a new size and nothing change
var contentRect2 = CGRectZero
for 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 any help
Layout of ViewControl:
Upvotes: 0
Views: 905
Reputation: 62072
The short, but wrong answer to this question is to add an autolayout constraint to the tableview constraining the height to a constant size. You can set it to your best guess, but at runtime set it equal to the tableview's content size's height.
The problem with this approach is that it completely eliminates the benefit of tableview's and reusable cells. Table views are expressly designed to minimize the memory footprint of your view. It takes a lot more memory to store the view (cell) that represents your data than it does to keep track with data you're filling these cells in with.
So instead, the better solution is to use multiple sections within a table view. Using the example from your screenshot, your parent scroll view will be a tableview instead of a scroll view. It would have four sections. In the first section, we return a single row with the yellow view which is configured as a table view cell, and we do the same in the third & fourth sections. In the second section, we return however many rows you were using in the table view.
Alternatively, the yellow view could be thought as the table header, and the blue & red views become the table footer, with the rows in the middle.
Upvotes: 2