ios - Swift 4 UICollectionView 检测滚动结束

标签 ios swift xcode uicollectionview swift4

我的应用程序上有一个 Horizo​​ntal 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/

相关文章:

xcode - Swift 3 - 如何使用枚举原始值作为 NSNotification.Name?

ios - UITableView didSelectRowAt 不称为 iOS 10,但适用于 9.3.5

ios - FaSTLane 健身房失败退出状态 : 1 although Archive Succeeded

arrays - Swift 4 认为数组在不为空时为空

swift - 从 UIViewController 使用 Segue 打开 UITabController - 如何将第三个选项卡显示为默认选项卡?

Swift 扩展协议(protocol)

ios - CAGradientLayer Mask 在 Swift 中添加不透明度

iphone - 获取两个位置之间的路线方向

ios - 自定义高度未应用于 UITableView 原型(prototype)单元格

swift - 如何隐藏 example.app 之类的包文件?