UICollectionview 无法通过 selectItem 函数滚动到项目 IOS

UICollectionview not scroll to item by selectItem function IOS

我无法使用 collectionView 的这个功能滚动到项目

collectionView.selectItem(at: IndexPath(row: 10 , section: 0), animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally) 

这个 CollectionView 是水平的

您目前只是在 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)

谢谢大家。我用这段代码解决了。我的错误代码是 self.categoryItemsCollection.selectItem(at: IndexPath(row: indexPath.section, section: 0), animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally) 只需将滚动位置参数更改为 .center Horizo​​ntally self.categoryItemsCollection.selectItem(at: IndexPath(row: indexPath.section, section: 0), animated: true, scrollPosition: .centeredHorizontally)