0

I am trying to use two collection views inside of a view controller and I get this error message as a result? Can you point out what i am missing to correct this issue? Thanks in advance.. Below is my code in the view controller named "OtherViewController".

class OtherViewController : UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

//1. CREATE A VARIABLE FOR YELLOW ARRAY SO THAT I CAN DEFINE A STRING
var exploreArray = [String]()
var exploreHeader = [String]()
var yellowImages = [String]()
var explorecategories = [String]()


@IBOutlet weak var mycollectionView: UICollectionView!


var collectionViewA = UICollectionView()
let collectionViewB = UICollectionView()
let collectionViewAIdentifier = "yellowCell"
let collectionViewBIdentifier = "yellowCell2"

 override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view.

        collectionViewA.delegate = self
        collectionViewB.delegate = self

        collectionViewA.dataSource = self
        collectionViewB.dataSource = self


        self.view.addSubview(collectionViewA)
        self.view.addSubview(collectionViewB)

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

   if collectionView == self.collectionViewA {
         return exploreArray.count        }

    return 1 // Replace with count of your data for collectionViewB

} 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   if collectionView == self.collectionViewA{

        let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "yellowCell", for: indexPath) as! yellowCell
                // Set up cell

    //DISPLAY TITLE LABEL
    cellA.yLabel.text = exploreHeader[indexPath.row]

    //DISPLAY IMAGES
    cellA.yellowImages.image  = UIImage(named:yellowImages [indexPath.row])

    //DISPLAY DESCRIPTION
    cellA.yellowTextField.text = exploreArray [indexPath.row]

    return cellA
    }

    else {

       let cellB =  collectionView.dequeueReusableCell(withReuseIdentifier: "yellowCell2", for: indexPath) as! yellowCell2

    cellB.labe2.text = "Dolphin"

//just radom label to to see if cellB will appear in collection view

               return cellB
    }

    }

1 Answers1

0

I think the answers I got to this question may be helpful for your situation as well.

Specifically, where you have

var collectionViewA = UICollectionView()
let collectionViewB = UICollectionView()

it was recommended (by a couple of folks much smarter than I!) to use instead

var collectionViewA: UICollectionView!
var collectionViewB: UICollectionView!

Then in viewDidLoad(), along with setting the delegate and dataSource (as you do), add something like

let layoutA = UICollectionViewFlowLayout()                  
layoutA.itemSize = CGSize(width: 100, height: 100)

let layoutB = UICollectionViewFlowLayout()
layoutB.itemSize = CGSize(width: 100, height: 100)

collectionViewA = UICollectionView(frame: self.view.frame, collectionViewLayout: layoutA)
collectionViewB = UICollectionView(frame: self.view.frame, collectionViewLayout: layoutB)

And then finally after adding the subviews,

collectionViewLeft.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewLeftIdentifier)
collectionViewRight.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewRightIdentifier)

The full code is shown in the answers to the linked question. (That uses left & right instead of A & B, but the same ideas hopefully apply.)

Community
  • 1
  • 1
ConfusionTowers
  • 911
  • 11
  • 34