-1

I am receiving this error and I checked every answer there is online on how to solve this and it does not work for me. I tried cleaning the build folder, I do not have a xib file, reuploading the collectionviewcell file, and I have all of the correct code. I have three collectionviews, and two of them work while the other one does not. This is my whole collectionview code for this class:

It does not work for collectionViewThree, collectionView and collectionViewTwo work perfectly! SleepThreeCollectionViewCell does exist!

@IBOutlet weak var collectionViewTwo: UICollectionView!
@IBOutlet weak var collectionViewThree: UICollectionView!
var arr = ["Quiet Time", "The Soft Lullaby", "", "", ""]
var imgArr = ["baby", "lulaby", "", "", ""]
var des = ["This is a soothing melody tone in for calming.", "Soft relaxing lullaby music featuring orchestra.", "", "", ""]
var arrTwo = ["Quiet Time", "The Soft Lullaby", "", "", ""]
var imgArrTwo = ["baby", "lulaby", "", "", ""]
var desTwo = ["This is a soothing melody tone in for calming.", "Soft relaxing lullaby music featuring orchestra.", "", "", ""]
var arrThree = ["Quiet Time", "The Soft Lullaby", "", "", ""]
var imgArrThree = ["baby", "lulaby", "", "", ""]
var desThree = ["This is a soothing melody tone in for calming.", "Soft relaxing lullaby music featuring orchestra.", "", "", ""]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == self.collectionView {
        return arr.count 
    }
    else if collectionViewTwo == self.collectionView{
        return arrTwo.count
    }
    return arrThree.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if self.collectionView == collectionView{
        i = indexPath.row
        let popupContentController = storyboard?.instantiateViewController(withIdentifier: "PlayerViewController") as! PlayerViewController
        popupContentController.popupItem.title = arr[i]
        popupContentController.popupItem.subtitle = des[i]
        popupContentController.popupItem.image = UIImage(named: imgArr[i])
        popupContentController.i = i
        tabBarController?.presentPopupBar(withContentViewController: popupContentController, animated: false, completion: nil)
    }else{
        print("Still Working")
    }
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if self.collectionView == collectionView{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sleep", for: indexPath) as! SleepCollectionViewCell
        cell.img.image = UIImage(named: imgArr[indexPath.row])
        cell.label.text = arr[indexPath.row]
        return cell
    }else if self.collectionView == collectionViewTwo{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "sleepTwo", for: indexPath) as! SleepTwoCollectionViewCell
        cell.imgTwo.image = UIImage(named: imgArrTwo[indexPath.row])
        cell.labelTwo.text = arrTwo[indexPath.row]
        return cell
    }
    else{
        let cell: SleepThreeCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "third", for: indexPath) as! SleepThreeCollectionViewCell
        cell.imgThree.image = UIImage(named: imgArrThree[indexPath.row])
        cell.labelThree.text = arrThree[indexPath.row]
        return cell
    } 
}
var i = 0
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionViewTwo.delegate = self
    collectionViewTwo.dataSource = self
    collectionViewThree.register(UINib(nibName: "SleepThreeCollectionViewCell", bundle: Bundle(for: SleepThreeCollectionViewCell.self) ), forCellWithReuseIdentifier: "third")
    collectionViewThree.delegate = self
    collectionViewThree.dataSource = self
}
  • For what purpose you have this line "collectionViewThree.register(UINib(nibName: "SleepThreeCollectionViewCell", bundle: Bundle(for: SleepThreeCollectionViewCell.self) ), forCellWithReuseIdentifier: "third")" ? You should remove it, if you are using this cell from storyboard – Mikhail Vasilev Dec 06 '20 at 19:35
  • I solved this problem, instead of creating multiple collection views, I created a table and put collection view inside table. – Benjamin Sloutsky Dec 07 '20 at 00:37

1 Answers1

0

I solved this problem, instead of creating multiple collection views, I created a table and put collection view inside table.