Good afternoon everyone. I have a collection with variable cell height. When scrolling a collection, the height of the content does not match the height of the cell. Tell me how to solve this problem?
This is how I create a collection.
// MARK: - UI Fabric.
private extension CollectionViewController {
func createCollectionView() -> UICollectionView {
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 16
layout.minimumLineSpacing = 16
layout.scrollDirection = .vertical
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.showsVerticalScrollIndicator = false
collectionView.translatesAutoresizingMaskIntoConstraints = false
return collectionView
}
}
This is delegate gor setting size cell.
// MARK: - CollectionView Flow Layout.
extension CollectionViewController: UICollectionViewDelegateFlowLayout {
/// Setting cell sizes.
func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
delegateSettingCell.calculateCellSize(screenWidth: view.bounds.width, index: indexPath.row)
}
}
Here I am calculating the cell size.
func calculateCellSize(screenWidth: CGFloat, index: Int) -> CGSize {
let item = modelForDisplay[index].addonDetail.count
let heightCell = CGFloat((item * 44) + 44)
let sizeCellWidth = (view.bounds.width - 32)
return CGSize(width: sizeCellWidth, height: heightCell)
}