Swift 4 UICollectionView detect end of scrolling

前端 未结 5 1527
野趣味
野趣味 2021-02-04 13:13

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 w

相关标签:
5条回答
  • 2021-02-04 13:44

    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
        }
    }
    
    0 讨论(0)
  • 2021-02-04 13:49

    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)
               }    }
    
    0 讨论(0)
  • 2021-02-04 13:50

    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
    
        }
    }
    
    0 讨论(0)
  • 2021-02-04 13:53

    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
    
    0 讨论(0)
  • 2021-02-04 13:53

    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
      }
    
    0 讨论(0)
提交回复
热议问题