How to populate a collection view from the ground up - swift - programmatically

145 views Asked by At

I'm developing a chat view controller and in doing so I need to present the latest messages whenever I load the controller: I'm using a collectionView with cells layout one on top of the other.

This Is the method I implement and it works, but is not as good as I want it to be:

  1. load small bunch of data (20 messages)

  2. then scroll to bottom with this function:

     let section = max(collectionView.numberOfSections - 1, 0)
     let row = max(collectionView.numberOfItems(inSection: section) - 1, 0)
     let lastIndexPath = IndexPath(row: row, section: section)
    
     self.collectionView.scrollToItem(at: lastIndexPath, at: .top, animated: true)
    

Beside the fact that whenever the view loads I need to see the scrolling animation which is annoying I also don't want to load unnecessary messages.

Is there a way to populate the collection view starting from the bottom? and populating "from the ground up" :)

I also made a lot of research online but did't find anything useful and practical.

For example this question suggests to rotate of 180° the collection view, I tried it but it gives me some bugs with the cells whenever I dismiss dinamically the keyboard. Also is annoying the fact that the scroll indicator is on the left instead of the right.

I'm curious to hear your point of view.

0

There are 0 answers