Swift 4 UICollectionView检测滚动结束 [英] Swift 4 UICollectionView detect end of scrolling

查看:250
本文介绍了Swift 4 UICollectionView检测滚动结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用上有一个Horizontal UICollectionView,我想在用户向左拖动时到达UICollectionView的末尾(或接近末尾)时加载更多数据.

I have an Horizontal UICollectionView on my app and I want to load more data when the user reaches the end (or nearly to the end) of UICollectionView while dragging on the left.

我正在使用Swift4.我找到了一些Swift 3解决方案,但它们对我不起作用.

I'm using Swift 4. I found some Swift 3 solutions but they do not work for me.

我当前的代码是:

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])
}

推荐答案

您可以使用集合视图的cellForItem或willDisplayItem方法.检查是否显示了最后一个单元格,并加载数据.例如:

You may use cellForItem or willDisplayItem methods of collection view. Check if the last cell is being displayed, and load your data. For example:

迅速:

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:

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

    }
}

这篇关于Swift 4 UICollectionView检测滚动结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆