I have a TableView that's constantly changing (cells are inserted and removed) inside a ScrollView. I have disabled scrolling for the TableView because there's a MapView above the TableView and I want to scroll the entire View, not just the TableView when the user scrolls up. Since cells are constantly being inserted and deleted into the TableView, the height for the TableView won't be fixed for the entire time the app is running.
I'm trying to constantly set the height of the TableView to match the height of the sum of the content inside by doing,
func someFunc() {
// call viewDidAppear to update TableView
viewDidAppear(true)
// Calculate the size of the ScrollView
var y: CGFloat = 10
y = mapView.bounds.size.height + tableView.bounds.size.height + 10
var sz = scrollView.bounds.size
sz.height = y
scrollView.contentSize = sz
}
// Update the TableView on main thread
override viewDidAppear(animated: Bool) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
var frame: CGRect = self.tableView.frame
frame.size.height = self.tableView.contentSize.height
self.tableView.bounds.size.height = frame.size.height
})
}
However, tableView.bounds.size.height is not getting updated to the new height of content. Actually it updates sometimes, maybe once every few calls. I'm almost certain it's because dispatch_asyn() is an asynchronous method. So, I might need to return in the method to solve this, but I'm not sure how.
Am I doing this correctly to accomplish what I need? Is there a better way to do this? Any help is greatly appreciated.