abdullaziz aman abdullaziz aman - 3 months ago 36
Swift Question

Set 3 UICollectionView in one UIViewController

I'm trying to set 3 UICollectionView in one UIViewController
and I create 3 different outlet as you see:

class myViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var myCollectionView1: UICollectionView!
@IBOutlet weak var myCollectionView2: UICollectionView!
@IBOutlet weak var myCollectionView3: UICollectionView!


and for each of them

override func viewDidLoad() {
super.viewDidLoad()

self.myCollectionView1.delegate = self
self.myCollectionView1.dataSource = self

self.myCollectionView2.delegate = self
self.myCollectionView2.dataSource = self

self.myCollectionView3.delegate = self
self.myCollectionView3.dataSource = self

}


and for UICollectionView DataSource protocol, for the Section

func numberOfSections(in collectionView: UICollectionView) -> Int {

if (collectionView == self.myCollectionView1) {

return 0

} else if (collectionView == self.myCollectionView2) {

return 0

} else if (collectionView == self.myCollectionView3) {

return 0

}

return 0

}


for the numberOfItemsInSections

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

if (collectionView == self.myCollectionView1) {

return self.allName.count

} else if (collectionView == self.myCollectionView2) {

return self.pharmacyName.count

} else if (collectionView == self.myCollectionView3) {

return self.clinicName.count

}

return 0

}


and for the cellAtIndexPath

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if (collectionView == myCollectionView1) {

let cell1: allBrandCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell1", for: indexPath) as! allBrandCollectionViewCell

cell1.name.text = allName[indexPath.row]
cell1.phoneNumber.text = allPhoneNumber[indexPath.row]

return cell1

} else if (collectionView == myCollectionView2) {

let cell2: pharmacyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell2", for: indexPath) as! pharmacyCollectionViewCell

cell2.name.text = pharmacyName[indexPath.row]
cell2.phoneNumber.text = pharmacyPhoneNumber[indexPath.row]

return cell2


} else if (collectionView == myCollectionView3) {

let cell3: clinicCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell3", for: indexPath) as! clinicCollectionViewCell

cell3.name.text = clinicName[indexPath.row]
cell3.phoneNumber.text = clinicPhoneNumber[indexPath.row]

return cell3

}

}


so the problem right now when I try to build the project they show me error
in cellForRowAtIndexPath:


Missing return in a function expected to return 'UICollectionViewCell'


enter image description here

Answer
  • "cellForItemAt" has a non optional UICollectionViewCell return type. But your code does not return a cell if none of the condition matches.
  • You can return an empty UICollectionViewCell object if none of the conditon mathes.

Corrected code below:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if (collectionView == myCollectionView1) {

        let cell1: allBrandCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell1", for: indexPath) as! allBrandCollectionViewCell

        cell1.name.text = allName[indexPath.row]
        cell1.phoneNumber.text = allPhoneNumber[indexPath.row]

        return cell1

    } else if (collectionView == myCollectionView2) {

        let cell2: pharmacyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell2", for: indexPath) as! pharmacyCollectionViewCell

        cell2.name.text = pharmacyName[indexPath.row]
        cell2.phoneNumber.text = pharmacyPhoneNumber[indexPath.row]

        return cell2


    } else if (collectionView == myCollectionView3) {

        let cell3: clinicCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell3", for: indexPath) as! clinicCollectionViewCell

        cell3.name.text = clinicName[indexPath.row]
        cell3.phoneNumber.text = clinicPhoneNumber[indexPath.row]

        return cell3

    }
   return UICollectionViewCell()
}