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

查看:74
本文介绍了如何实现水平无限滚动 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天全站免登陆