Swift 4 UICollectionView detect end of scrolling

前端 未结 5 1674
野趣味
野趣味 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: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
    
        }
    }
    

提交回复
热议问题