I have a table view (1) with a header. I'd like to put another table view (2) inside that header. The problem is that the second table view's cells are dynamic, so I need to make sure that the first table view's header is also dynamic.
I've followed this tutorial to make the first table view's header dynamic:
https://useyourloaf.com/blog/variable-height-table-view-header/
But the table view header doesn't even show when I run the simulator, and I believe this has to do with the fact that the second table view's cells are dynamic, and therefor not assigning a height to the first table view's header.
So my question is, what is causing the header to not show and how can I fix it?
Here is my code:
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var firstTableView: UITableView!
@IBOutlet weak var headerView: UIView!
@IBOutlet weak var secondTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
firstTableView.delegate = self
firstTableView.dataSource = self
secondTableView.delegate = self
secondTableView.dataSource = self
// Make the first table view's cells dynamic
firstTableView.rowHeight = UITableView.automaticDimension
firstTableView.estimatedRowHeight = 50
// Make the second table view's cells dynamic
secondTableView.rowHeight = UITableView.automaticDimension
secondTableView.estimatedRowHeight = 50
// Register the custom xib for the first table view
firstTableView.register(UINib(nibName: "FirstTableViewCell", bundle: nil), forCellReuseIdentifier: "FirstTableViewCell")
// Register the custom xib for the second table view
secondTableView.register(UINib(nibName: "SecondTableViewCell", bundle: nil), forCellReuseIdentifier: "SecondTableViewCell")
}
/**
Used to resize the table view header.
Source: https://useyourloaf.com/blog/variable-height-table-view-header/
*/
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
guard let headerView = firstTableView.tableHeaderView else {
return
}
let size = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
if (headerView.frame.size.height != size.height) {
headerView.frame.size.height = size.height
firstTableView.tableHeaderView = headerView
firstTableView.layoutIfNeeded()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.firstTableView {
return 100
}
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.firstTableView {
// Logic to create new cell
} else if tableView == self.secondTableView {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "SecondTableViewCell", for: indexPath) as? SecondTableViewCell else {
fatalError("The dequeued cell is not an instance of SecondTableViewCell.")
}
cell.initialize()
return cell
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
}