unixb0y
unixb0y

Reputation: 1048

UICollectionViewCell containing a UICollectionView

I have a CollectionView which has cells that in turn also contain a little 3-paged CollectionView themselves.

That way, the user can scroll in the main view and also swipe the little cells left and right.

My problem is that I want these cells to be initialized in a way that their CollectionView always shows the center item (item 1) as default.

When the main view loads, I do this:

DispatchQueue.main.async {
    for cell in self.CollectionView.visibleCells {
        (cell as! PersonCell).CollectionView.scrollToItem(at: IndexPath(item: 1), at: [], animated: false)
    }
    self.CollectionView.alpha = 1
}


Which works fine, but when I scroll the main CollectionView down, of course new cells are initialized and they all show their item 0 in their own CollectionView.

What would be a good solution for this?
Thank you in advance!!

Upvotes: 0

Views: 50

Answers (1)

unixb0y
unixb0y

Reputation: 1048

This seems to work, but not sure if 100% "correct" or always working:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    (cell as? PersonCell)?.collectionView.scrollToItem(at: IndexPath(item: 1), at: [], animated: false)
}

Upvotes: 1

Related Questions