How to Scroll to a Particluar Index in Collection View in Swift

How to scroll to a particluar index in collection view in swift

In viewDidLoad of your controller, you can write the code for scrolling to a particular index path of collection view.

self.collectionView.scrollToItem(at:IndexPath(item: indexNumber, section: sectionNumber), at: .right, animated: false)

Scroll to particular index UICollectionView

I solved this by not using the above post but instead came up with this solution that worked for me:

  internal func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if !onceOnly {

let indexToScrollTo = IndexPath(item: 12, section: 0)
self.collectionView.scrollToItem(at: indexToScrollTo, at: .left, animated: false)
onceOnly = true
}
}

Hope it helps others out :D

UICollectionView auto scroll to cell at IndexPath

I've found that scrolling in viewWillAppear may not work reliably because the collection view hasn't finished it's layout yet; you may scroll to the wrong item.

I've also found that scrolling in viewDidAppear will cause a momentary flash of the unscrolled view to be visible.

And, if you scroll every time through viewDidLayoutSubviews, the user won't be able to manually scroll because some collection layouts cause subview layout every time you scroll.

Here's what I found works reliably:

Objective C :

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];

// If we haven't done the initial scroll, do it once.
if (!self.initialScrollDone) {
self.initialScrollDone = YES;

[self.collectionView scrollToItemAtIndexPath:self.myInitialIndexPath
atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
}
}

Swift :

 override func viewWillLayoutSubviews() {

super.viewWillLayoutSubviews()

if !self.initialScrollDone {

self.initialScrollDone = true
self.testNameCollectionView.scrollToItem(at:selectedIndexPath, at: .centeredHorizontally, animated: true)
}

}

UICollectionView scroll to item not working with horizontal direction

For this part:

collectionView.scrollToItem(at: i, at: .top, animated: true)

When the scroll direction is horizontal you need to use at: left, at: right or at: centeredHorizontally. at: top is for vertical direction.

Scrolling to a collectionView to a specie Index

what ViewController event method are you using to run your code?

Anyway I could scroll it placing your code in the ViewController event method viewDidAppear method.

class CollectionViewController: UICollectionViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

let cellToSelect = IndexPath(item: 99, section: 0)

self.collectionView?.scrollToItem(at: cellToSelect, at: [.centeredHorizontally, .centeredVertically], animated: true)
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! RepsCellView
cell.setupViews() // added this line
return cell
}
}


Related Topics



Leave a reply



Submit