Dynamic collectionView in UITableViewCell
Recently I'm building a page like this:
There's a dynamic collectionView
in every UITableViewCell
, for the self-sizing UITableViewCell
s to work, override systemLayoutSizeFitting
:
class TableViewCell: UITableViewCell {
@IBOutlet weak var collectionView: UICollectionView!
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
collectionView.collectionViewLayout.invalidateLayout()
collectionView.layoutIfNeeded()
collectionView.setNeedsLayout()
let height = collectionView.collectionViewLayout.collectionViewContentSize.height
return CGSize(width: UIScreen.main.bounds.width, height: height)
}
}