如何实现水平无限滚动UICollectionView? [英] How to implement horizontally infinite scrolling UICollectionView?

查看:289
本文介绍了如何实现水平无限滚动UICollectionView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现水平和无限滚动的UICollectionView吗?

I want to implement UICollectionView that scrolls horizontally and infinitely?

推荐答案

如果您的数据是静态数据,并且想要某种循环行为,则可以执行以下操作:

If your data is static and you want a kind of circular behavior, you can do something like this:

var dataSource = ["item 0", "item 1", "item 2"]

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return Int.max // instead of returnin dataSource.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let itemToShow = dataSource[indexPath.row % dataSource.count]
    let cell = UICollectionViewCell() // setup cell with your item and return
    return cell
}

基本上,您在集合视图中说您拥有大量的单元格(Int.max不会是无限的,但是可以解决问题),并且您可以使用%运算符访问数据源.在我的示例中,我们将以"item 0","item 1","item 2","item 0","item 1","item 2" ...结束.

Basically you say to your collection view that you have a huge number of cells (Int.max won't be infinite, but might do the trick), and you access your data source using the % operator. In my example we'll end up with "item 0", "item 1", "item 2", "item 0", "item 1", "item 2" ....

我希望这会有所帮助:)

I hope this helps :)

这篇关于如何实现水平无限滚动UICollectionView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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