0

So my UICollectionView has 2 cells:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        var imageCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? CollectionViewCell
        var videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellVideo", forIndexPath: indexPath) as? CollectionViewCell

        if (imageCell != nil) { // Image Cell
            println("image cell exist")
            return imageCell!
        }

        if (videoCell != nil) { // Video Cell
            println("video cell exist")
            return videoCell!
        }
    }

Somehow I get error Missing return in a function expected to return 'UICollectionViewCell'. Why is that when I have return in both the if? I might think it is because I use return 2 times, or?

EDIT:

I also tried this, but does not work either:

var myUrl = "Url to file from web"
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        var imageCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? CollectionViewCell
        var videoCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellVideo", forIndexPath: indexPath) as? CollectionViewCell

        if myUrl.rangeOfString(".png") != nil{ // If string contains .png it should use a imageCell
            println("exists")
            return imageCell!
        }
        if myUrl.rangeOfString(".mp4") != nil{ // If string contains .mp4 it should use a videoCell
            println("exists")
            return videoCell!
        }
    }
Kim Phoe
  • 25
  • 9

2 Answers2

1

You might get a situation where imageCell == nil and videoCell == nil.

Your code will currently not return anything in that case. If you are certain one of these will not be nil maybe try something like

if (imageCell != nil) { // Image Cell
            println("image cell exist")
            return imageCell!
}
else{ // Video Cell
            println("video cell exist")
            return videoCell!
}
Friggles
  • 634
  • 5
  • 7
  • That would not work i think. Because i am thinking something like if string is equal to "imageFile", it will use the imageCell, and if the string is equal to "videoFile", it will use videoCell. How would i do that? – Kim Phoe Nov 25 '15 at 21:25
  • The compiler can see a way that the method can finish without anything returning so you need to return something for when your string isnt either a image or video. – Friggles Nov 25 '15 at 21:36
0

Your only returning a cell if a condition is met. If neither of the if statements run then nothing is being returned at all. (A cell must be returned in all cases)

Make sure you have a cell returned if neither of the conditions is met, this can often be thought of as a default return value.

Try using the if, else if, else in your method so you will always return a cell, and you will prevent an unnecessary conditional check if your imageCell != null.

As for your actual method, I'm not sure this is the proper implementation precise for a cellForItemAtIndexPath method. But I'll just answer your question... :D

Tim Davis
  • 9
  • 2