【问题标题】:How to scroll to first cell, while swiping the last cell on CollectionView?如何滚动到第一个单元格,同时在 CollectionView 上滑动最后一个单元格?
【发布时间】:2018-12-29 11:02:35
【问题描述】:

我正在开发一个包含许多单元格的 CollectionView。而我想要做的是:

  • 当用户在collectionView 的最后一个单元格上,并且当用户滑动查看下一个单元格时,我希望这个collectionView 从头开始​​重新启动。 (基本上,我需要一个无限循环)

希望我能正确解释。等待您的解决方案。谢谢。

【问题讨论】:

标签: ios swift uicollectionview uicollectionviewcell collectionview


【解决方案1】:

试试这个。

import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate=self;
        collectionView.dataSource=self;
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100;
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath)
        cell.backgroundColor = UIColor.red
        if indexpath.row==99{
            self.btnScroll()
           }
        return cell
    }

    func btnScroll() {
        collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: UICollectionView.ScrollPosition.top, animated:true)
    }
}

【讨论】:

  • 不幸的是,这是滚动到第一项,我需要在最后一项之后显示第一项并继续滚动,就像我在该 collectionView 中有更多内容一样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 2019-12-21
  • 1970-01-01
相关资源
最近更新 更多