Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start UICollectionView at a specific indexpath

I currently have a collection view that does horizontal paging where each cell is fullscreen. What I want to do is for the collectionview to start at a specific index when it shows.

Right now I'm using scrollToItemAtIndexPath:atScrollPosition:animated: with animated set to NO but that still loads the first index first before it can scroll to the specific item. It also seems I can only use this method in ViewDidAppear so it shows the first cell and then blinks to the cell that I want to show. I hide this by hiding the collection view until the scroll has finished but it doesn't seem ideal.

Is there any better way to do this other than the way I described it?

Thanks!

like image 269
Zack Avatar asked Aug 06 '13 17:08

Zack


People also ask

What is the difference between Uitableview and UICollectionView?

Tableiw is a simple list, which displays single-dimensional rows of data. It's smooth because of cell reuse and other magic. 2. UICollectionView is the model for displaying multidimensional data .

How does UICollectionView work?

The collection view presents items onscreen using a cell, which is an instance of the UICollectionViewCell class that your data source configures and provides. In addition to its cells, a collection view can present data using other types of views.


2 Answers

So I solved this a different way, using the UICollectionViewDelegate method and a one-off Bool:

Swift 2:

var onceOnly = false  internal func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {     if !onceOnly {         let indexToScrollTo = NSIndexPath(forRow: row, inSection: section)         self.problemListCollectionView.scrollToItemAtIndexPath(indexToScrollTo, atScrollPosition: .Left, animated: false)         onceOnly = true     }  } 

Swift 3:

  var onceOnly = false    internal func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {     if !onceOnly {       let indexToScrollTo = IndexPath(item: row, section: section)       self.problemListCollectionView.scrollToItem(at: indexToScrollTo, at: .left, animated: false)       onceOnly = true     }   } 

This code is executed before any animation occurs (so it really loads to this point), which is better than attempting to call in viewDidAppear, and I didn't have success with it in viewWillAppear.

like image 94
sschale Avatar answered Oct 16 '22 23:10

sschale


To solve this problem I partially used the greenhouse answer.

/// Edit var startIndex: Int! = 0  override func viewWillAppear(animated: Bool) {     super.viewWillAppear(animated)      collectionView.setNeedsLayout()     collectionView.layoutIfNeeded()      collectionView.scrollToItemAtIndexPath(         NSIndexPath(forItem: 0, inSection: startIndex),         atScrollPosition: .None,         animated: false)  } 

The problem seems to be in the wrong collectionView size. After setting the layout scrollToItemAtIndexPath produces the needed result.

It also seems that this problem only persists when a Collection View is used inside a UIViewController.

like image 34
victor.vasilica Avatar answered Oct 17 '22 00:10

victor.vasilica