-1

I am having some trouble with my cellForItem because I don't know how to load 3 collection views simultaneously. This is what I tried but I get the error "Missing return in a function expected to return 'UICollectionViewCell".

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

if collectionView == self.lostCollectionView {

        let lostcell: LostCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Lostcell", for: indexPath) as! LostCollectionViewCell

        lostcell.set(post: posts[indexPath.row])

        //Make TextView Clickable
        lostcell.phoneLostTextView.isEditable = false;
        lostcell.phoneLostTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber

        return lostcell
    }

    if collectionView == self.foundCollectionView {

        let foundcell: FoundCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Foundcell", for: indexPath) as! FoundCollectionViewCell

        foundcell.set(postfound: postsfound[indexPath.row])

        //Make TextView Clickable
        foundcell.phoneFoundTextView.isEditable = false;
        foundcell.phoneFoundTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber

        return foundcell
    }

    if collectionView == self.adoptionCollectionView {

        let adoptioncell: AdoptionCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Adopotioncell", for: indexPath) as! AdoptionCollectionViewCell

        adoptioncell.set(postadoption: postsadoption[indexPath.row])

        //Make TextView Clickable
        adoptioncell.phoneAdoptionTextView.isEditable = false;
        adoptioncell.phoneAdoptionTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber

        return adoptioncell
    }
}
  • Compare [Missing return UITableViewCell](https://stackoverflow.com/questions/30189505/missing-return-uitableviewcell) for a similar problem. – Martin R Oct 19 '18 at 18:44
  • just remove the last if condition `if collectionView == self.adoptionCollectionView {`. If your code get there is because It is a adoptionCollectionView. – Leo Dabus Oct 19 '18 at 19:31

3 Answers3

2

Your function has 3 ifs. If they all fail, the function does not return anything. That's the reason the Swift compiler is complaining.

You could add return UICollectionViewCell() at the bottom of the function.

Also, a switch statement is more appropriate for this situation.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
1

Basically you will need to include your code inside a switch statement like this:

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

    switch collectionView {
    case self.lostCollectionView:
        let lostcell: LostCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Lostcell", for: indexPath) as! LostCollectionViewCell

        lostcell.set(post: posts[indexPath.row])

        //Make TextView Clickable
        lostcell.phoneLostTextView.isEditable = false;
        lostcell.phoneLostTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber

        return lostcell

    case self.foundCollectionView:
        let foundcell: FoundCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Foundcell", for: indexPath) as! FoundCollectionViewCell

        foundcell.set(postfound: postsfound[indexPath.row])

        //Make TextView Clickable
        foundcell.phoneFoundTextView.isEditable = false;
        foundcell.phoneFoundTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber

        return foundcell

    case self.adoptionCollectionView:
        let adoptioncell: AdoptionCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Adopotioncell", for: indexPath) as! AdoptionCollectionViewCell

        adoptioncell.set(postadoption: postsadoption[indexPath.row])

        //Make TextView Clickable
        adoptioncell.phoneAdoptionTextView.isEditable = false;
        adoptioncell.phoneAdoptionTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber

        return adoptioncell

    default:
        return UICollectionViewCell()
    }
}
Raul Mantilla
  • 334
  • 1
  • 5
0

As mentioned by meaning-matters, this is due to the missing cell, in case none of the 3 ifs are satisfied.

You could add a final fallback cell at the bottom of the function as meaning-matters suggested.

Or, you can go with another similar approach which I would personally prefer:

// Inside your **cellForItemAt** function.

switch collectionView {
    case lostCollectionView:
        // Replace this with your corresponding code.
        return LostCollectionViewCell()

    case foundCollectionView:
        // Replace this with your corresponding code.
        return FoundCollectionViewCell()

    case adoptionCollectionView:
        // Replace this with your corresponding code.
        return AdoptionCollectionViewCell()

    default:
        return UICollectionViewCell()
}

I personally find switch-case to be a neater solution for these kind of cases.

D V
  • 348
  • 2
  • 10
  • Hi! Thank you for your reply. I used your switch code and placed the ifs code within but when I placed both the return lostcell and then return LostCollectionViewCell() I get that "Code after 'return' will never be executed". So how do i run the cell? – Ximena Flores de la Tijera Oct 19 '18 at 19:10
  • @AvaEamer Actually, you should remove the **return LostCollectionViewCell()** and the other 2 valid cases that you will replace your code with, after including your code there. That was just a placeholder code for your reference. – D V Oct 19 '18 at 19:20