Ios UICollectionView加载完成后,如何启动该函数?

Ios UICollectionView加载完成后,如何启动该函数?,ios,swift,Ios,Swift,加载UICollectionView后如何使用returnleft func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomePageTitleCell", for

加载
UICollectionView
后如何使用
returnleft

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  
}

不在cellForItemAt中工作只能用于在表视图中创建单元格。它将为表中的每个单元格调用一次(不一定是一次调用,而是在滚动到表视图时调用)

因此,将代码移到下面更合适的位置,如
viewDidLoad

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

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

看起来与此问题类似:您需要更精确地说明“已完成加载”的含义。加载所有集合视图时没有特定的点。它故意只加载它认为在任何给定时间都需要的部分(以及在iOS 10中加载的部分发生了更改)。除非所有视图同时显示在屏幕上,否则加载其他部分时将取决于滚动。你需要明确你所说的“不工作”是什么意思。你期望什么?到底发生了什么?如果这真的是关于滚动到一个单元格,这是一个重复的问题,并有一个答案。
let returnleft = IndexPath(row: 0, section: 0)
self.collectionView.scrollToItem(at: returnleft, at:UICollectionViewScrollPosition.left, animated: true)