ThundercatChris
ThundercatChris

Reputation: 491

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)

})

Upvotes: 4

Views: 8465

Answers (6)

Adeel Ur Rehman
Adeel Ur Rehman

Reputation: 566

You need to call [self.view layoutIfNeeded] before calling scrollToBottomWithoutAnimation

Upvotes: 0

Eray Alparslan
Eray Alparslan

Reputation: 814

@Politta's answer is correct. Here's how you can implement what you want in Objective C:

- (void)scrollToBottomWithoutAnimation{
    NSInteger lastVisibleRowIndex = (NSInteger)self.myItems.count - 1;
    NSIndexPath *lastVisibleIndexPath = [NSIndexPath indexPathForRow: lastVisibleRowIndex inSection:0];
    [self.collectionView scrollToItemAtIndexPath:lastVisibleIndexPath atScrollPosition: UICollectionViewScrollPositionCenteredVertically animated:NO];
}

In your viewWillAppear method:

dispatch_async(dispatch_get_main_queue(), ^{
    [self scrollToBottomWithoutAnimation];
});

Upvotes: 0

Politta
Politta

Reputation: 410

The best solution I found was to use DispatchQueue.

Swift 4.2:

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if let index = items.firstIndex(of: preSelectedItem) {
        DispatchQueue.main.async {
            self.collectionView.scrollToItem(at: IndexPath(item: index, section: 0), at: .top, animated: false)
        }
    }
}

Upvotes: 0

Hiren Panchal
Hiren Panchal

Reputation: 3023

Swift 3.0 OR up

add "view.layoutIfNeeded()" before implementing scrollToItem method, Ideally in viewWillAppear

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
view.layoutIfNeeded()
  colView.scrollToItem(at: IndexPath(item: 4, section: 0), at: .centeredHorizontally, animated: true)}

Upvotes: 18

Willjay
Willjay

Reputation: 6469

I use dispatch_async to update the UI and it works.

let numberOfSections = self.collectionView.numberOfSections()
let numberOfRows = self.collectionView.numberOfItemsInSection(numberOfSections-1)

    if numberOfRows > 0 {
        dispatch_async(dispatch_get_main_queue(), {
            let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
            self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
        })
    }

Upvotes: 7

ThundercatChris
ThundercatChris

Reputation: 491

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

Upvotes: 0

Related Questions