I'm using UITableViewController
and put UICollectionView
in second row in Table View Cell. The problem is how I can get heightForRowAt dynamically.
I have tried using return UITableViewAutomaticDimension
. But not working.
Any help is really appreciated.
viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = self.tableView.rowHeight
self.tableView.rowHeight = UITableViewAutomaticDimension
}
UITableViewController
Option 1
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.tableView.bounds.width + 15
}
Option 2
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Currently, I hard code the number of returns.
UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 15
}
UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let layout = collectionViewLayout as! UICollectionViewFlowLayout
layout.minimumLineSpacing = 2.5
layout.minimumInteritemSpacing = 2.5
itemWidth = (collectionView.bounds.width - 5.0) / 3.0
return CGSize(width: itemWidth, height: itemWidth)
}
Here is the image of UI using return self.tableView.bounds.width + 15
:
Here is the image of UI using return UITableViewAutomaticDimension
:
Here is the image of Constraints :