displaying collectionView Cells starting from bottom - swift programmatically

347 Views Asked by At

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?

0

There are 0 best solutions below