本文介绍了UICollectionView完成加载后如何启动功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
}
加载 UICollectionView 后如何使用 returnleft ?
let returnleft = IndexPath(row: 0, section: 0) self.collectionView.scrollToItem(at: returnleft, at: UICollectionViewScrollPosition.left, animated: true)在 cellForItemAt
推荐答案cellForItemAt 仅用于在表格视图中创建单元格.将为表中的每个单元格调用一次(不一定要一次调用,而是滚动到表视图时调用).
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).
因此,将代码移到下面更合适的位置,例如 viewDidLoad .
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)更多推荐
UICollectionView完成加载后如何启动功能?
发布评论