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

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

问题描述

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

例如下图:

一般你要在numberOfItemsInSection里设置数据源的items个数,怎么回事?,你设置items总数然后懒加载什么的?>

提前致谢.

解决方案

使用 scrollViewDidScroll 函数像上面的 crazy_phage 一样,你可以观察到什么时候最终到达 CollectionView 的最后,然后你可以更新 numberOfRowInSections 并按以下方式调用 reloadData :

class CollectionSampleViewController: UICollectionViewController {私人让重用标识符1 =单元格"私有变量 numberOfItemsPerSection = 9覆盖 func viewDidLoad() {super.viewDidLoad()}覆盖 func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}覆盖 func numberOfSectionsInCollectionView(collectionView: UICollectionView) ->整数{返回 1}覆盖 func collectionView(collectionView: UICollectionView, numberOfItemsInSection 部分: 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屋!

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