Search code examples
swiftanimationscrolluicollectionviewuicollectionreusableview

UICollectionview scrollToItemAtIndexPath, not loading visible cells until animation complete


I have a UICollectionView with 142 Cells. 7.5 are visible at any one time.

I'm moving a cell from indexPath 0 to say 100. But I also want to scroll to that new position.

The code below works fine. But it animates the move and scroll, but then loads the cells in front of and behind the central/moved cell afterwards. I think its because the cells are reusable. but with 142, I can't pre-load all of them

Its not the nicest effect, I would like to pre-load the cells surrounding the new position, 4 before and 4 after indexPath 100, and then see the animation of the move and scroll. can you help please?

UIView.animateWithDuration(animationSpeed, animations: {() -> Void in
    self.collectionView.layoutIfNeeded()
    self.collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem:self.indexPathSelected.row, 
                                                 inSection:0), 
                                                 atScrollPosition: .CenteredHorizontally, 
                                                 animated: true)

})

Solution

  • The only answer I have found, is to add a few seconds to the animation. That way the cells are loaded when the scroll arrives