Swift 4 UICollectionView detect end of scrolling

2020-06-13 02:29发布

问题:

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.

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

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

回答1:

You may use cellForItem or willDisplayItem methods of collection view. 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:

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

    }
}


回答2:

Implement method scrollViewDidScroll of UIScrollViewDelegate:

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


回答3:

Take one bool variable and one integer variable for page number like this:

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

in cellForItemAt method add below code:

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:

self.isDataLoading = false


回答4:

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


回答5:

Add this in your collectionView willDisplayCell delegate

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

//Load more data & reload your collection view
  }