UICollectionview不会通过selectItem函数IOS滚动到项目

时间:2018-05-01 14:00:19

标签: swift io uicollectionview uikit uicollectionviewcell

我无法使用collectionView

的此功能滚动到Item
collectionView.selectItem(at: IndexPath(row: 10 , section: 0), animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally) 

此CollectionView是水平的

2 个答案:

答案 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参数更改为.centeredHorizo​​ntally self.categoryItemsCollection.selectItem(at: IndexPath(row: indexPath.section, section: 0), animated: true, scrollPosition: .centeredHorizontally)