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

        if collectionView == thisSeasonCollectionView {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as UICollectionViewCell

            let imageView = cell.viewWithTag(1) as! UIImageView

            let url = NSURL(string: URLArrayStringThisSeason[indexPath.row])

            let placeholderImage = UIImage(named: "Rectangle")!

            let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
                size: imageView.frame.size,
                radius: 0
            )

            imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
            )
            cell.layer.cornerRadius = 3.0

            return cell
        }
        else if collectionView == whatsNewCollectionView {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as UICollectionViewCell

            let imageView = cell.viewWithTag(1) as! UIImageView

            let url = NSURL(string: URLArrayStringRecents[indexPath.row])

            let placeholderImage = UIImage(named: "Rectangle")!

            let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
                size: imageView.frame.size,
                radius: 0
            )

            imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
            )

            cell.layer.cornerRadius = 3.0

            return cell
        }
    }

Why doesn't this work? I wish to try to link 3 collection view each from a different tableView Cell to this swift file but it seems like there can only be two. The code works fine if I replace 'else if' with 'else' for some reason.

Edit:

How do I edit the return of cell count:

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

        if collectionView == thisSeasonCollectionView {
            return URLArrayStringThisSeason.count
        }else if collectionView == whatsNewCollectionView {
            return URLArrayStringRecents.count
        }else if collectionView == labelCollectionView {
            return URLArrayStringLabel.count
        }

    }
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
Ler Ws
  • 317
  • 5
  • 17

5 Answers5

1

Change your code to the following:

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as UICollectionViewCell

    if collectionView == thisSeasonCollectionView {
        let imageView = cell.viewWithTag(1) as! UIImageView

        let url = NSURL(string: URLArrayStringThisSeason[indexPath.row])

        let placeholderImage = UIImage(named: "Rectangle")!

        let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
            size: imageView.frame.size,
            radius: 0
        )

        imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
        )
        cell.layer.cornerRadius = 3.0
    }
    else if collectionView == whatsNewCollectionView {
        let imageView = cell.viewWithTag(1) as! UIImageView

        let url = NSURL(string: URLArrayStringRecents[indexPath.row])

        let placeholderImage = UIImage(named: "Rectangle")!

        let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
            size: imageView.frame.size,
            radius: 0
        )

        imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
        )

        cell.layer.cornerRadius = 3.0
    }

    return cell
 }

Make the return cell at the end since you will only hit one of your statements and all statements needs to return a cell, so no need to return cell on three different places, it´s enough to place it in the end. And it´s not necessary to add the let cell = collectionView.dequeu... two times, it´s enough with one time.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
0

Not all control paths in above code return a cell. Add another return after 'else if' body

Khurram Shehzad
  • 261
  • 3
  • 12
  • that didnt work too. There's nothing to return outside the else if body. And all bodies have a return cell. – Ler Ws Oct 15 '16 at 15:23
  • @LerWs then change the `else if ...` to `else`. – luk2302 Oct 15 '16 at 15:30
  • I need to have three collection views. I'm setting it to else if so that I can add another else if for another collectionView – Ler Ws Oct 15 '16 at 15:31
0

Your code boils down to

function doSomething(a : Int) -> String {
    if a == 1 {
        return "something";
    } else if a == 2 {
        return "somethingElse";
    }
}

The compiler complains because what happens if a is 3? The method will not return anything. You might know a will never be 3, but the compiler does not know that.

Therefore either change the else if ... to a simple else to make the function always return something OR return something after the else if.

luk2302
  • 55,258
  • 23
  • 97
  • 137
0

According to the comments you posted on the other answers, you have three (and potentially more) collection views and wish to have a code structure like this:

if collectionView == collectionView1 {
    // code that returns some cell
} else if collectionView == collectionView2 {
    // code that returns some other cell
} else if collectionView == collectionView3 {
    // code that returns yet another cell
}

And the reason why you use an else if instead of an else is because you want to add more else ifs later on.

Then I think, calling fatalError is the best solution for you.

if collectionView == collectionView1 {
    // code that returns some cell
} else if collectionView == collectionView2 {
    // code that returns some other cell
} else if collectionView == collectionView3 {
    // code that returns yet another cell
}
fatalError("Please add another else if to handle the cell of collection view!")

Not only does this removes the "no all code path returns a value" error, it also reminds you if you forgot to add an else if!

This solution can also be applied to the other datasource methods as well!

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Thanks guys, I'll try the switch method too next but for now I've found a pretty good method:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let cellCount = 0

        if collectionView == thisSeasonCollectionView {
            let cellCount = URLArrayStringThisSeason.count
            return cellCount
        }else if collectionView == whatsNewCollectionView {
            let cellCount = URLArrayStringRecents.count
            return cellCount
        }else if collectionView == labelCollectionView {
            let cellCount = URLArrayStringLabel.count
            return cellCount
        }
        return cellCount
    }
Ler Ws
  • 317
  • 5
  • 17