0

I'm trying to display cells in a collection view one in top of the other. the layout I want to achieve is described properly in this delegate layout function:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
    return CGSize(width: self.collectionView.frame.width, height: 190)
     
}

So let's make an example that we have 20 cells to display. I want that the first cells displayed are the last ones.

Now I'm starting displaying the first cells and then I scroll to the last ones in this way:

self.collectionView.scrollToItem(at: collectionView.lastIndexpath(), at: .top, animated: true)

extension UICollectionView {
func lastIndexpath() -> IndexPath {
    let section = max(numberOfSections - 1, 0)
    let row = max(numberOfItems(inSection: section) - 1, 0)
    return IndexPath(row: row, section: section)
}
}

How can I start displaying the collection view from the ending elements without having to display first the first elements?

StackGU
  • 868
  • 9
  • 22
  • try using `animated: false` in the arguments to `scrollToItem()` – Gereon Mar 13 '21 at 14:07
  • I thought about this option too but the cells are called both in ```cellForItem at: IndexPath``` and in ```willDisplay``` which is something I want to prevent. – StackGU Mar 13 '21 at 14:20

0 Answers0