4

I have added a UICollectionVIew to a vertical Stack View. I connected both datasource and Delegate from storyboard and added the delegate/datasource from the code as well.

However, the following method is not being called at all:

internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { ... }

But, the methods

func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return 2
    }

are being called.

So, I moved the UICollectionVIew out of the vertical Stack View and it was working then. But, the method is not being called when the UICollectionVIew is in the vertical Stack View.

How can I solve this?

Illep
  • 16,375
  • 46
  • 171
  • 302

1 Answers1

0

Same happen with me in past with TableView but not with UIStackView

If numberOfSections and numberOfItemsInSection called and cellForItemAt not called then only possible reason your CollectionView height is 0

Because of that your cell didn't get enough space to show cell

You can Check same thing by adding height of your collection view to 0 in to the case where you said I moved the UICollectionVIew out of the vertical Stack View and it was working then.

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98