我的应用程序上有一个 Horizontal
UICollectionView
,我想在用户拖动时到达 UICollectionView 的末尾(或接近末尾)时加载更多数据左边。
我正在使用 Swift 4。我找到了一些 Swift 3 解决方案,但它们不适合我。
我当前的代码是:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.videoViewModel.images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
cell.imgImage.image = self.videoViewModel.images[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
updateVideo(data: self.videoViewModel.relatedVideos[indexPath.row])
}
最佳答案
您可以使用 Collection View 的 cellForItem 或 willDisplayItem 方法。检查是否显示了最后一个单元格,并加载您的数据。例如:
swift :
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if (indexPath.row == dataSource.count - 1 ) { //it's your last cell
//Load more data & reload your collection view
}
}
objective-C :
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == dataSource.count - 1 ) { //it's your last cell
//Load more data & reload your collection view
}
}
关于ios - Swift 4 UICollectionView 检测滚动结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48705430/