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
Path Extension and Mime Type of File in Swift
Make Uicollectionview Zoomable
Simulate Universal Gravitation for Two Sprite Kit Nodes
iOS Autolayout and Uitoolbar/Uibarbuttonitems
What Is the Swift Equivalent of Makeobjectsperformselector
Why Force Unwrapping Is Required in Case of Enum and Switch
How to Enable "Tap and Slide" in a Uislider
Audiokit: Using the New Aksequencer with Any Variety of the Callback Instruments
Accessing Variables from Another Viewcontroller in Swift
How to Get Notified in Contact Changed Event in iOS
Application(...Continue Useractivity...) Method Not Called in iOS 13
Why Would a 'Scheduledtimer' Fire Properly When Setup Outside a Block, But Not Within a Block
Nsnumberformatter with Comma Decimal Separator