I am implementing self sizing UITableViewCell containing UICollectionView which extends its height based on the number of items received from API.
The views are structured like this
UITableView
UITableViewCell
UICollectionView
UICollectionViewCell
UICollectionViewCell
UICollectionViewCell
When the UICollectionView receives new data, UITableViewCell frame is updated with the following code
layoutIfNeeded()
collectionView.reloadData()
collectionView.collectionViewLayout.invalidateLayout()
collectionView.layoutIfNeeded()
collectionView.snp.updateConstraints { make in
make.height.equalTo(collectionView.collectionViewLayout.collectionViewContentSize).priority(.high)
}
The problematic part is adding pagination on scroll to the bottom of the UICollectionView. The self sizing code causes that willDisplay cell is called for each cell of the UICollectionView causing // Fetch more data request to trigger, which fetches new data, updates UICollectionView height, triggering the API request again, and it's an infinite loop.
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if indexPath.item == collectionView.numberOfItems(inSection: indexPath.section) - 1 {
// Fetch more data
}
}
Any idea how to avoid this cycle? Thanks.