我无法使用collectionView
的此功能滚动到ItemcollectionView.selectItem(at: IndexPath(row: 10 , section: 0), animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
此CollectionView是水平的
答案 0 :(得分:1)
您目前只是在indexPath上选择集合视图项,这不会滚动到项目。 您需要滚动所选项目的位置。添加以下滚动方法:
collectionView.selectItem(at: IndexPath(row: 10 , section: 0), animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
collectionView.scrollToItem(at: IndexPath(row: 10 , section: 0), at: .centeredHorizontally, animated: true)
答案 1 :(得分:-1)
谢谢大家。我用这个代码解决了。我的错误代码是self.categoryItemsCollection.selectItem(at: IndexPath(row: indexPath.section, section: 0), animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
,只需将scrollPosition参数更改为.centeredHorizontally self.categoryItemsCollection.selectItem(at: IndexPath(row: indexPath.section, section: 0), animated: true, scrollPosition: .centeredHorizontally)