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