2

I'm working on a CollectionView with many cells in it. And what I am trying to do is:

  • When the user is on the last cell of collectionView, and when the user swipes to see the next cell, I want this collectionView to restarts from the beginning. ( Basically, I need an endless loop )

Hope I could explain it correctly. Waiting for your solutions. Thank you.

Mr. T.
  • 93
  • 10

1 Answers1

0

Try this.

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)
    }
}
Sagar koyani
  • 407
  • 2
  • 11
  • unfortunately, this is scrolling to first item, what I need is showing the first item after the last one and continue to scroll like I have more contents in that collectionView – Mr. T. Jan 04 '19 at 08:48