Load More data on Scroll Demand on CollectionView
Swift 3 : CollectionView Delegate method
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if indexPath.row == numberofitem.count - 1 { //numberofitem count
updateNextSet()
}
}
func updateNextSet(){
print("On Completetion")
//requests another set of data (20 more items) from the server.
}
I do this thing through CollectionViewDelegate method like:
- (void)collectionView:(UICollectionView *)collectionView
willDisplayCell:(UICollectionViewCell *)cell
forItemAtIndexPath:(NSIndexPath *)indexPath{
if indexPath.row == numberOfitem.count-1 && !self.waiting {
waiting = true;
self.loadMoreData()
}
}
-(void)loadMoreData(){
// After getting the response then
[numberOfitem addObjects : your response in term of array];
waiting = false;
}
In swift 4.0
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// Note: ‘isWating’ is used for checking the paging status Is currency process or not.
if indexPath.section == self.dataSoruce.list.count - 2 && !isWating {
isWating = true
self.pageNumber += 1
self.doPaging()
}
}
private func doPaging() {
// call the API in this block and after getting the response
self.dataSoruce.append(newData);
self.tableView.reloadData()
self.isWating = false // it means paging is done and the user can able request another page request via scrolling the table view at the bottom.
}