简体   繁体   English

uicollection视图在底部滚动时加载更多数据。 iOS Swift

[英]uicollection view load more data at bottom scroll. iOS Swift

I want to load more data in UICollectionView when I scroll bottom to the UICollectionView . 我想加载更多的数据UICollectionView当我滚动底部到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 问题1- >向下滚动时如何加载更多数据

Question 2 -> If I will able to get more data on scrolling to the bottom so how should I add in same mutable array. 问题2- >如果滚动到底部将能够获得更多数据,那么如何添加相同的可变数组。 so it load whole data. 因此它会加载整个数据。

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). 将结果添加到您的数组中,然后重新加载集合视图(或仅将其告知所插入的项目)。

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. 答案1:有一个名为SVPullToRefresh的库,它将帮助您实现所需的内容。 Try add addInfiniteScrollingWithActionHandler in you UICollectionView for load more. 尝试添加addInfiniteScrollingWithActionHandlerUICollectionView负载更多。

Answer 2: Yes you are supposed to add the newly loaded data to the mutable array. 答案2:是的,您应该将新加载的数据添加到可变数组中。 Doing this will help you to scroll up and find the previously loaded array items. 这样做将帮助您向上滚动并查找以前加载的数组项。

Hope this will help. 希望这会有所帮助。

Swift 3 and Swift 4 : CollectionView Delegate method Swift 3和Swift 4 :CollectionView委托方法

1) When scroll hit the bottom you can trigger updateNextSet() using willDisplay function 1)当滚动到达底部时,您可以使用willDisplay函数触发updateNextSet()

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() 2)将新数据附加到现有数组并重新加载集合视图collectionView.reloadData()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM