使用垂直无限滚动在 UICollectionView 中显示需求中的单元格 [英] Showing cells in demands in UICollectionView with vertical infinite scroll
问题描述
我想知道如何使用 UICollectionView
实现 按需加载 的预期效果,就像在亚马逊应用中进行搜索时一样,它只返回一个滚动结束之前的项目数,然后设置一个加载指示器以加载更多单元格并继续直到显示搜索中的所有产品.
如下图所示:
一般你要在numberOfItemsInSection
中设置,数据源的item个数,怎么弄的,你设置items的总数然后懒加载什么的?p>
提前致谢.
使用像上面的 crazy_phage 那样的 scrollViewDidScroll
函数,你可以观察到最终到达 CollectionView 的最后,然后你可以更新 numberOfRowInSections
并按以下方式调用 reloadData
:
类 CollectionSampleViewController: UICollectionViewController {私人让重用标识符1 =细胞"私有变量 numberOfItemsPerSection = 9覆盖 func viewDidLoad() {super.viewDidLoad()}覆盖 func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}覆盖 func numberOfSectionsInCollectionView(collectionView: UICollectionView) ->诠释{返回 1}覆盖 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) ->诠释{返回 numberOfItemsPerSection}覆盖 func scrollViewDidScroll(scrollView: UIScrollView) {让 offsetY = scrollView.contentOffset.y让 contentHeight = scrollView.contentSize.height如果 offsetY >contentHeight - scrollView.frame.size.height {numberOfItemsPerSection += 6self.collectionView.reloadData()}}}
当 reloadData
函数调用它时,滚动保持在同一个位置,并且它被加载的新数据.
在上面的代码中,您可以使用活动指示器视图或您想要添加到代码中的任何其他内容.
I would like to know how to achieve using a UICollectionView
the desired effect of loading in demand like in the Amazon app when you make a search, it returns only a number of items until the scroll ends, then a loading indicator it set to load more cells and go on until all the products in the search it's showed.
Something like the following picture for example :
Normally you have to set in the numberOfItemsInSection
, the number of items of the data source, how it's the way?, you set the total of items and then load it lazy or something?
Thanks in advance.
Using the scrollViewDidScroll
function like crazy_phage did above you can observe when finally reach the final of the CollectionView then you can update the numberOfRowInSections
and call the reloadData
in the following way :
class CollectionSampleViewController: UICollectionViewController {
private let reuseIdentifier1 = "Cell"
private var numberOfItemsPerSection = 9
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfItemsPerSection
}
override func scrollViewDidScroll(scrollView: UIScrollView) {
let offsetY = scrollView.contentOffset.y
let contentHeight = scrollView.contentSize.height
if offsetY > contentHeight - scrollView.frame.size.height {
numberOfItemsPerSection += 6
self.collectionView.reloadData()
}
}
}
When the reloadData
function its called the scroll remains in the same place and the new data it's loaded.
In the above code you can use an Activity Indicator View or anything else you want to add to your code.
这篇关于使用垂直无限滚动在 UICollectionView 中显示需求中的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!