0

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.

StackGU
  • 868
  • 9
  • 22
  • 1
    Have you tried `self.collectionView.setContentOffset(CGPoint(x: 0, y: CGFloat.infinity), animated: false)` to scroll to bottom as soon as you call `collectionView.reloadData` you can also try setting animated property to `false` `self.collectionView.scrollToItem(at: lastIndexPath, at: .top, animated: false)` to avoid scrolling animation – Sandeep Bhandari Mar 14 '21 at 13:54
  • I tried .infinity but it gives me an error, while setting the scroll animation to ```false``` is firstly showing top messages and then you can see the choppy move to the bottom, which is annoying – StackGU Mar 14 '21 at 14:17
  • whats the error you see when you use .infinity? rotating 180 degree seems too much of hack to me :| – Sandeep Bhandari Mar 14 '21 at 14:18
  • 1
    No I tried mixing your two suggestions ```self.collectionView.scrollToItem(at: lastIndexPath, at: .top, animated: false)``` as soon as I call reload data and it works, thank you very much !! @SandeepBhandari :) – StackGU Mar 14 '21 at 14:24

0 Answers0