Amg91
Amg91

Reputation: 164

Height on UICollectionViewCell error when reload

I am basing the question as a branch of this question

The autosize on ViewCells works perfect. But when I reload my CollectionView, the height return 1.

Code as follows.

extension SaleViewController: CollectionViewFlowLayoutDelegate{
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        guard let flowLayout = self.myCollectionView.collectionViewLayout as? CustomLayout else {
            return
        }
        flowLayout.invalidateLayout()
    }

    func height(at indexPath: IndexPath) -> CGFloat {
        guard let cell = self.myCollectionView.cellForItem(at: indexPath) else {
            return 1
        }
        cell.layoutIfNeeded()
        //get calculated cell height
        return cell.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
    }
}

What is missing or what else should I reconsider to keep my autoheight?

Thanks in advance.

Upvotes: 2

Views: 737

Answers (1)

Bhavesh.iosDev
Bhavesh.iosDev

Reputation: 942

Check your CustomLayout there will be cache that store layoutAttribute for your cell.ex : private var cache: [IndexPath : UICollectionViewLayoutAttributes] = [:]

you have to Clear this Cache before reloading

guard let flowLayout = self.myCollectionView.collectionViewLayout as? CustomLayout else {
            return
        }

flowLayout.cache.removeAll()
flowLayout.delegate = self
CollectionView.reloadData()

Upvotes: 1

Related Questions