简体   繁体   English

Swift 4 UICollectionView检测滚动结束

[英]Swift 4 UICollectionView detect end of scrolling

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. 我的应用程序上有一个Horizontal UICollectionView ,当用户在左侧拖动时到达UICollectionView的末尾(或接近末尾)时,我想加载更多数据。

I'm using Swift 4. I found some Swift 3 solutions but they do not work for me. 我正在使用Swift 4.我找到了一些Swift 3解决方案,但它们对我不起作用。

My current code is: 我目前的代码是:

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

You may use cellForItem or willDisplayItem methods of collection view. 您可以使用集合视图的cellForItem或willDisplayItem方法。 Check if the last cell is being displayed, and load your data. 检查是否显示最后一个单元格,然后加载数据。 For example: 例如:

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: 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

    }
}

Implement method scrollViewDidScroll of UIScrollViewDelegate : UIScrollViewDelegate实现方法scrollViewDidScroll

var isLoading: Bool = false

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let contentOffsetX = scrollView.contentOffset.x
    if contentOffsetX >= (scrollView.contentSize.width - scrollView.bounds.width) - 20 /* Needed offset */ {
        guard !self.isLoading else { return }
        self.isLoading = true
        // load more data
        // than set self.isLoading to false when new data is loaded
    }
}

Take one bool variable and one integer variable for page number like this: 为页码取一个bool变量和一个整数变量,如下所示:

var isDataLoading = false
var pageCount:Int = 1 // Pass this page number in your api

in cellForItemAt method add below code: 在cellForItemAt方法中添加以下代码:

if !isDataLoading && indexPath.row == videoViewModel.count - 1 {
            isDataLoading = true
            pageCount += 1
// add you api call code here with pageCount
        }

once you get your data from api set isDataLoading bool to false like below: 一旦从api中获取数据,将isDataLoading bool设置为false,如下所示:

self.isDataLoading = false

This is simple login which will go down to last index...!!! 这是简单的登录,将归结为最后一个索引...... !!!

func ScrollEnd() {
          let lastSectionIndex = (self.collectionView?.numberOfSections)! - 1
         let lastItemIndex = (self.collectionView.numberOfItems(inSection: lastSectionIndex)) - 1
           let index =  IndexPath(item: lastItemIndex, section: lastSectionIndex)
           if messages.count != 0{
               self.collectionView!.scrollToItem(at: index, at: UICollectionView.ScrollPosition.bottom, animated: false)
           }    }

Add this in your collectionView willDisplayCell delegate 在您的collectionView willDisplayCell委托中添加此项

if (indexPath.row == dataSource.count - 1 ) {
 //it's your last cell

//Load more data & reload your collection view
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM