UICollectionView加载完成后如何启动功能?

2024-01-09

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  
}

我该如何使用returnleft加载后UICollectionView?

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

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

不在cellForItemAt


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

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

let returnleft = IndexPath(row: 0, section: 0)
self.collectionView.scrollToItem(at: returnleft, at:UICollectionViewScrollPosition.left, animated: true)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UICollectionView加载完成后如何启动功能? 的相关文章

随机推荐