0

i want to make dynamic height collectionView like below picture, i use collectionView in tableView.

so i set collectionView's data in tableViewDataSource.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell") as? MyTableViewCell else {
        return UITableViewCell()
    }
    
    cell.setLabelArray(label[indexPath.row])
    cell.titleLabel.text = titleArray[indexPath.row]
    return cell
}

and then in tableViewCell, i adjust collectionView's height.

class MyTableViewCell: UITableViewCell {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var myCollectionView: MyCollectionView!
@IBOutlet weak var collectionViewHeight: NSLayoutConstraint!

var labelArray: [String]? {
    didSet {
        myCollectionView.reloadData()
        let height = self.myCollectionView.collectionViewLayout.collectionViewContentSize.height
        self.collectionViewHeight.constant = height
    }
}
...

but layout's contentSize is always different when i use flowLayout (like below last picture)

how can i get correct contentSize?? .......

enter image description here enter image description here enter image description here

1 Answers1

0

I think you should use sizeForItemAt method for collectionview layout method. Don't forget to add delegate UICollectionViewDelegateFlowLayout to viewcontroller. You can see this answer How to set cell spacing and UICollectionView - UICollectionViewFlowLayout size ratio?