如何解决collectionView不会scrollToItem-快速-以编程方式
我以这种方式在UIView中设置了一个UICollectionview:
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,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(item: 0,section: counter)
向后(两次)。试试
IndexPath(item: counter,section: 0)
在两个地方。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。