使用垂直无限滚动在UICollectionView中显示需求中的单元格 [英] Showing cells in demands in UICollectionView with vertical infinite scroll

查看:111
本文介绍了使用垂直无限滚动在UICollectionView中显示需求中的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用 UICollectionView 实现按需加载所需的效果,就像在亚马逊应用中一样搜索,它只返回一些项目,直到滚动结束,然后一个加载指示器,它设置为加载更多的单元格,然后继续,直到搜索它显示的所有产品。

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.

如下图所示:

通常你必须在 numberOfItemsInSection 中设置数据源的项目数,这是怎么回事?,你设置项目的总数,然后加载它懒惰或什么?

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?

提前致谢。

推荐答案

使用像上面的crazy_phage一样的 scrollViewDidScroll 函数,你可以观察到最终到达CollectionView的最后一个然后你就可以了更新 nu mberOfRowInSections 并按以下方式调用 reloadData

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆