Steven Oh
Steven Oh

Reputation: 394

Controlling two different collectionview

I am currently trying to control two different collection view. I have also embedded buttons in each of the collection views I want to make each of them contain a set of values from an array. This is my code so far, I don't know what I should actually do to achieve that:

import UIKit
class MyButtonCell: UICollectionViewCell{
    @IBOutlet weak var buttonOne: UIButton!
    @IBOutlet weak var targetButton: UIButton!
    
    var callback: (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
        
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    func commonInit() -> Void {
        contentView.layer.borderWidth = 1
        contentView.layer.borderColor = UIColor.black.cgColor
    }
    
    @IBAction func buttonTapped(_ sender: UIButton) {
        callback?()
    }
}

class StevenViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    let buttonTitles: [String] = [
        "4", "6", "7", "8"
    ]
    
    var targetButtonTitles: [String] = [
        "", "", "", ""
    ]
    
    var current:String = ""
    @IBOutlet var collectionView: UICollectionView!
    
    @IBOutlet var targetCollection: UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        targetCollection.delegate = self
        targetCollection.dataSource = self
        
        collectionView.delegate = self
        collectionView.dataSource = self
        
        
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return buttonTitles.count
        
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell
        let targetCell = targetCollection.dequeueReusableCell(withReuseIdentifier: "myCellID", for: indexPath) as! MyButtonCell

           // set the button title (and any other properties)
        
           cell.buttonOne.setTitle(buttonTitles[indexPath.item], for: [])
        

           targetCell.targetButton.setTitle(self.targetButtonTitles[indexPath.item], for: [])
        
           // set the cell's callback closure
           cell.callback = {
               print("Button was tapped at \(indexPath)")
               self.targetButtonTitles.append(self.buttonTitles[indexPath.item])
               print(self.targetButtonTitles)
           
               
               // do what you want when the button is tapped
           }
           

           return targetCell
        
    }
}

buttonTitles and targetButtonTitles are the two arrays I want each of my collectionviews to contain.

This is what it looks like right now - both are showing the same array. I understand that I probably should have different identifier ID for the cells, but once I do that, it gave me an error saying "could not dequeue a view of kind: UICollectionElementKindCell with identifier target"

enter image description here

Upvotes: 1

Views: 69

Answers (1)

Rohit Parihar
Rohit Parihar

Reputation: 352

In UICollectionViewDelegate & UICollectionViewDataSource

You can use this code snippet to identify the current UICollectionView.

 if collectionView == [YOUR_COLLECTIONVIEW_QUTLET] {
       
      
 }

Upvotes: 1

Related Questions