collectionView没有';t scrollToItem-swift-以编程方式
我将UICollectionview以以下方式设置在UIView中:collectionView没有';t scrollToItem-swift-以编程方式,swift,uicollectionview,collectionview,indexpath,Swift,Uicollectionview,Collectionview,Indexpath,我将UICollectionview以以下方式设置在UIView中: videoCollectionView.delegate = self videoCollectionView.dataSource = self self.playerView.insertSubview(videoCollectionView, belowSubview: additionalItemsView) videoCollectionView.fillSuperview() 这就是数据源和委托方法的实
videoCollectionView.delegate = self
videoCollectionView.dataSource = self
self.playerView.insertSubview(videoCollectionView, belowSubview: additionalItemsView)
videoCollectionView.fillSuperview()
这就是数据源和委托方法的实现方式
extension CardView : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let numberOfURLS = cardModel?.urlStrings?.count
return numberOfURLS!
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let videoCell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCellIdentifier", for: indexPath)
videoCell.backgroundColor = UIColor.random()
return videoCell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.bounds.width, height: self.bounds.height)
}
}
从上面的代码可以看出,每个单元格都是collectionView.frame
我想让collectionView单元格水平显示,并能够在其中滚动
这是我实现的在单元格中水平滚动的代码:
fileprivate func goToNextVideo(){
counter = counter+1
counter = min(cardModel!.urlStrings!.count-1, counter)
videoCollectionView.scrollToItem(at: IndexPath(item: 0, section: counter), at: .left, animated: true)
}
fileprivate func goToPreviousVideo(){
counter = counter - 1
counter = max(0, counter)
videoCollectionView.scrollToItem(at: IndexPath(item: 0, section: counter), at: .left, animated: true)
}
我不知道是什么原因,collectionView没有滚动到所需的单元格,我在控制台中收到以下警告:
Warning: Invalid IndexPath <NSIndexPath: 0xa5dbc31284d24cfa> {length = 2, path = 1 - 0} specified - will use a contentOffset of {0,0} as a fallback value.
警告:指定的IndexPath{length=2,path=1-0}无效-将使用contentOffset{0,0}作为回退值。
在我看来
IndexPath(item: 0, section: counter)
是向后的(两次)。试一试
在这两个地方。它没有像以前那样给我警告,但它还没有滚动到索引路径的项目,我还注意到collectionView的contentSize是(355.0、1667.0),这意味着它是垂直开发的,而不是我想保存的水平开发,我也想到了,但那是另一回事。这是水平还是垂直滚动的集合视图取决于您。您没有显示任何配置
videoCollectionView
的代码,因此我没有理由相信您这样做是正确的。我通过添加自定义UICollectionViewFlowLayout()并将其scrollDirection属性设置为horizontalYes来解决此问题。我不确定“自定义”是什么意思;UICollectionViewFlowLayout有一个scrollDirection
属性,您可以设置它。您不需要为此目的创建子类。
IndexPath(item: counter, section: 0)