Search code examples
swiftbuttonuicollectionviewactioncollectionview

scroll back and forth multiple times with the buttons


The back and forth buttons only work once and i can't scroll back and forth multiple times with the buttons.

I am attempting to scroll the collectionview to the next cell once the cells are no longer visible each time I tap the button.

The code I have now is not working correctly.

How can I fix this so that it scrolls correctly when I tap the button?

Code:


import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    var labelArray = ["User1", "User2", "User3", "User4", "User5", "User6", "User7", "User8", "User9"]
    
    var numberLeft = -200.0
    var numberRight = 200.0
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    
   override func viewDidLoad() {
        super.viewDidLoad()
    
    collectionView.delegate = self
    collectionView.dataSource = self
    
    print("NUMBERLEFT: \(numberLeft) - NUMBERRIGHT: \(numberRight)")
    
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return labelArray.count
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
        
        cell.label.text = labelArray[indexPath.row]
        

       
       collectionView.contentInset = UIEdgeInsets(top: 0, left: CGFloat(numberLeft), bottom: 0, right: CGFloat(numberRight))
        
        print("NUMBERLEFT: \(numberLeft) - NUMBERRIGHT: \(numberRight)")
        
        return cell
        
    }

    
    
    
    @IBAction func buttonLeft(_ sender: Any) {
        numberLeft = numberLeft + 100.0
        collectionView.reloadData()
     }
    
    @IBAction func buttonRight(_ sender: Any) {
        numberRight = numberRight + 100.0
        collectionView.reloadData()
    }
    
   
    

}

enter image description here


Solution

  • If you want to scroll the collection view then you should be using the function scrollRectToVisible... https://developer.apple.com/documentation/uikit/uiscrollview/1619439-scrollrecttovisible

    import UIKit
    
    class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
        
        var labelArray = ["User1", "User2", "User3", "User4", "User5", "User6", "User7", "User8", "User9"]
        
        @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 labelArray.count
        }
        
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
            
            cell.label.text = labelArray[indexPath.row]
            
            return cell
        }
    
        // Change the button actions to just scroll the collection view rather than having o reload it.
        @IBAction func buttonLeft(_ sender: Any) {
            collectionView.scrollRectToVisible(collectionView.visibleRect.offsetBy(dx: -100, dy: 0), animated: true)
        }
        
        @IBAction func buttonRight(_ sender: Any) {
            collectionView.scrollRectToVisible(collectionView.visibleRect.offsetBy(dx: 100, dy: 0), animated: true)
        }
    }
    
    extension UIScrollView {
        var visibleRect: CGRect {
            CGRect(origin: contentOffset, size: bounds.size)
        }
    }
    

    Something like this anyway. You might want to tweak the amount it scrolls left and right.