Reputation: 994
I want to load more data in UICollectionView
when I scroll bottom to the UICollectionView
. I did not found any library for swift language. can anyone please tell me how can I do this?
Question 1 -> how to load more data when I scroll down
Question 2 -> If I will able to get more data on scrolling to the bottom so how should I add in same mutable array. so it load whole data.
Upvotes: 3
Views: 3690
Reputation: 1865
Swift 3 and Swift 4 : CollectionView Delegate method
1) When scroll hit the bottom you can trigger
updateNextSet()
usingwillDisplay
function
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.
}
2) Append your new data with existing array and reload collection view
collectionView.reloadData()
Upvotes: 2
Reputation: 706
Following are the answers to your questions.
Answer 1: There is a library called SVPullToRefresh which will help you achieve what you are looking for. Try add addInfiniteScrollingWithActionHandler
in you UICollectionView
for load more.
Answer 2: Yes you are supposed to add the newly loaded data to the mutable array. Doing this will help you to scroll up and find the previously loaded array items.
Hope this will help.
Upvotes: 0
Reputation: 119031
The collection view is a subclass of scroll view, so you can use the scroll delegate methods to find out when a scroll has happened / completed. When that happens you can check the content offset to determine if you're at the bottom or not.
When you are, and checking you aren't already loading more, you can start a new load. Add the results to your array and then reload the collection view (or tell it just about the inserted items).
Upvotes: 1