0

I am using a library called VegaScrollFlowLayout that is located on github. I am trying to give an exact index in my collectionView, and present the data starting from that index, but I keep getting a reset back to Index 0 and not Index 10.

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


    collectionView.scrollToItem(
        at: NSIndexPath(item: 10, section: 0) as IndexPath,
        at: [],
            animated: false)
  
    
 }

2 Answers 2

1

You should probably put your call to scrollToItem(at:at:animated:) in viewDidAppear(), not viewWillAppear(). In viewWillAppear() the collection view has not shown any cells yet. I don't think you'd need a delay that way.

Using delays is a fragile way of doing things. Better to move the code to the correct lifecycle method.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, after some digging around, I had to add a delay in the code.

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

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.1)
    {
        self.collectionView.scrollToItem(
        at: NSIndexPath(item: 15, section: 0) as IndexPath,
        at: [],
            animated: false)

    }
 }

This fixed the issue.

1 Comment

You should probably put your call to scrollToItem(at:at:animated:) in viewDidAppear(), not viewWillAppear(). In viewWillAppear()` the collection view has not shown any cells yet. I don't think you'd need a delay that way. (Although I haven't tested it.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.