0
votes

I've been having trouble trying to add a UICollectionView inside my view Controller. This is the error I'm getting when I try to run my app:

When I run my app Im getting this error: NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] instantiated view controller with identifier "UIViewController-UZJ-6r-Rwt" from storyboard "Main", but didn't get a UICollectionView.' What would be causing this error?

This is the code I have for the View Controller.

import UIKit

class ViewController: UICollectionViewController {

var names = ["a","d","f","f","h","u","p","s","l","g","y","h","l","k","d"]

var images = [UIImage(named: "title205571580"),UIImage(named: "title193064553"),UIImage(named: "title311505831"),UIImage(named: "title351533554"),UIImage(named: "title528311821"),UIImage(named: "title528311821"),UIImage(named: "title543192229"),UIImage(named: "title544380635"),UIImage(named: "title579492173"),UIImage(named: "title673858816"),UIImage(named: "title713937166"),UIImage(named: "title718520594"),UIImage(named: "title743923696"),UIImage(named: "title800939906"), UIImage(named: "title987804505")]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 15
}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "uicvcell", for: indexPath) as! UICVCell

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

    image.image = images[indexPath.row]

    let name = cell.viewWithTag(2) as! UILabel

    name.text = names [indexPath.row]

    return cell
}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if(indexPath.row == Int("0")){
        performSegue(withIdentifier: "0", sender: nil)
    }
    else if(indexPath.row == Int("1")){
        performSegue(withIdentifier: "1", sender: nil)
    }
    else if(indexPath.row == Int("2")){
        performSegue(withIdentifier: "2", sender: nil)
    }
    else if(indexPath.row == Int("3")){
        performSegue(withIdentifier: "3", sender: nil)
    }
    else if(indexPath.row == Int("4")){
        performSegue(withIdentifier: "4", sender: nil)
    }
    else if(indexPath.row == Int("5")){
        performSegue(withIdentifier: "5", sender: nil)
    }
    else if(indexPath.row == Int("6")){
        performSegue(withIdentifier: "6", sender: nil)
    }
    else if(indexPath.row == Int("7")){
        performSegue(withIdentifier: "7", sender: nil)
    }
    else if(indexPath.row == Int("8")){
        performSegue(withIdentifier: "8", sender: nil)
    }
    else if(indexPath.row == Int("9")){
        performSegue(withIdentifier: "9", sender: nil)
    }
    else if(indexPath.row == Int("10")){
        performSegue(withIdentifier: "10", sender: nil)
    }
    else if(indexPath.row == Int("11")){
        performSegue(withIdentifier: "11", sender: nil)
    }
    else if(indexPath.row == Int("12")){
        performSegue(withIdentifier: "12", sender: nil)
    }
    else if(indexPath.row == Int("13")){
        performSegue(withIdentifier: "13", sender: nil)
    }
    else if(indexPath.row == Int("14")){
        performSegue(withIdentifier: "14", sender: nil)
    }
    else if(indexPath.row == Int("15")){
        performSegue(withIdentifier: "15", sender: nil)
    }
    else if(indexPath.row == Int("16")){
        performSegue(withIdentifier: "16", sender: nil) }
    else if(indexPath.row == Int("17")){
        performSegue(withIdentifier: "17", sender: nil)
    }
    else if(indexPath.row == Int("18")){
        performSegue(withIdentifier: "18", sender: nil)



    }


}
}
3

3 Answers

4
votes

It looks like you're subclassing from UICollectionViewController. Is the VC in the storyboard that? Or just a regular UIViewController? If you created it in the storyboard as a UICollectionViewController, then the UICollectionView is already there. If you need to add the UICollectionView, then you probably created a regular UIViewController, and added in the UICollectionView afterwards- which is fine, but then you need to change your source to reflect that.

1
votes

What is missing is that in your storyboard you have used a UIViewController. Delete that and make sure you use a UICollectionViewController.

0
votes

Looks like your controller is missing it's collectionView property. What you need to do is set it as an outlet in your .storyboard file. If you want to set it from the code you will need to do something like this:

override func viewDidLoad() {
super.viewDidLoad()

// Create your custom collectionView.
let myCollectionView = MyCollectionView()
// Set it as outlet.
collectionView = myCollectionView
}