0 Replies
      Latest reply on Jun 9, 2018 12:56 PM by macuser1984
      macuser1984 Level 1 Level 1 (0 points)

        I was watching the WWDC 2018 session 225 A Tour of UICollectionView, and in the last section (starts around 28:30 in the video), it walked through how to animate changes to the collection view. One thing it didn't show is how to handle the animation for inserting an element in a part of the collection view that isn't currently visible (in my case, at the end of it). In saying that, I followed the session's recommendations, but I also added a call to scrollToItem(at:at:animated) at the end of the update code since without it, the collection view updates but doesn't scroll/show the newly added element. With that, it does smoothly show the newly added element at the end.


        Is this the proper thing to do to get it to show the newly added element with proper animation?


        Here's the relevant code from the action method of the button that triggers this:

        // (update the model's data - not shown)
        let newItemIndexPath = IndexPath(item: collectionView.numberOfItems(inSection: 0), section: 0)
        let update = { self.collectionView.insertItems(at: [newItemIndexPath]) }
        collectionView.scrollToItem(at: newItemIndexPath, at: .right, animated: true) // the line I'm wondering about