0
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomePageTitleCell", for: indexPath) as! HomePageTitleCell

   cell.news = homePageViewController.sliderList[indexPath.row]

   let returnleft = IndexPath(row: 0, section: 0)

   self.collectionView.scrollToItem(at: returnleft, at: UICollectionViewScrollPosition.left, animated: true)

   return cell  
}

How can I use returnleft after loading UICollectionView?

let returnleft = IndexPath(row: 0, section: 0)

self.collectionView.scrollToItem(at: returnleft, at: UICollectionViewScrollPosition.left, animated: true)

not working in cellForItemAt

Nitesh
  • 1,275
  • 2
  • 18
  • 45
  • Looks similar to this question: http://stackoverflow.com/questions/15985574/uicollectionview-auto-scroll-to-cell-at-indexpath – mlidal Jan 04 '17 at 07:55
  • You will need to be more precise about what you mean by "finished loading." There is no particular point when all of a collection view is loaded. It intentionally only loads portions it believes are needed at any given time (and which parts are loaded changed in iOS 10). When it loads other pieces will depend on scrolling, unless all of the view is on screen at the same time. You need to be precise about what you mean by "not working." What do you expect? What is actually happening? If this is really about scrolling to a cell, this is a duplicate question and has an answer. – Rob Napier Jan 05 '17 at 13:49

1 Answers1

0

cellForItemAt is to be used only to create the cells in your table view. It will be called once for each cell in your table (not necessarily at once, but as you scroll into the table view).

So move your code below in a more appropriate place, like viewDidLoad.

let returnleft = IndexPath(row: 0, section: 0)
self.collectionView.scrollToItem(at: returnleft, at:UICollectionViewScrollPosition.left, animated: true)
Bogdan Farca
  • 3,428
  • 21
  • 31