10

I have a collectionViewController that I want to display a bunch of custom UICollectionViewCells with some labels on them. Unfortunately whenever I try and access the custom UICollectionViewCell's label it causes a crash with:

Console

fatal error: Can't unwrap Optional.None

Window

Thread1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0)

I'm trying to access the label like so:

cell.name.text = names[indexPath!.item]

Perhaps this comes from my outlet label being nil? But looking around for answers nothing has worked, and because I'm not really sure what the issue is adding ?/! in my code isn't really helping.

MyCustomUICollectionViewController

class ScrambledTextCollectionViewController: UICollectionViewController {

    var names: String[] = ["Anna", "Alex", "Brian", "Jack"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView.registerClass(MyCustomCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView?) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView?, numberOfItemsInSection section: Int) -> Int {
        return names.count
    }

    override func collectionView(collectionView: UICollectionView?, cellForItemAtIndexPath indexPath: NSIndexPath?) -> UICollectionViewCell? {
        var cell = collectionView?.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as MyCustomCollectionViewCell

        cell.name.text = names[indexPath!.item]

        return cell
    }
}

MyCustomCollectionViewCell

class MyCustomCollectionViewCell: UICollectionViewCell {

    @IBOutlet var name: UILabel
    init(frame: CGRect) {
        super.init(frame: frame)                
    }
}
William Robinson
  • 1,028
  • 17
  • 32
  • Which line is the error occurring on? Have you added an exception breakpoint to try to find where the error is? – Fogmeister Jun 30 '14 at 14:31
  • cell.name.text = names[indexPath!.item] gets a Thread1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0) – William Robinson Jun 30 '14 at 14:34
  • 1
    Maybe I'm being stupid but you haven't defined the `var names` outside the scope of `viewDidLoad`. Surely you should define the var... `var names` in the scope of the whole object not just viewDidLoad. – Fogmeister Jun 30 '14 at 14:37
  • Ah whoops, thats because i've simplified the code for Stack Overflow and that array shouldn't be set there, i'll fix it, but it isn't the issue. – William Robinson Jun 30 '14 at 14:40
  • "Perhaps this comes from my outlet label being nil?" If `cell.name` is indeed `nil`, that's your problem. Can you log its value right before your crashing line? – Nate Cook Jun 30 '14 at 14:43
  • println(cell.word) returns nil, so you seem to be onto the answer, but how/where would I make it not nil? – William Robinson Jun 30 '14 at 14:46
  • Try replace exclamation mark with question tag names[indexPath?.item] – Greg Jun 30 '14 at 14:49
  • I don't believe this would fix my issue, it will crash even if i directly set the label cell.name.text = "Hello". What confuses me is that i've made a UITableView use a custom UITableViewCell in much the same way without issue. – William Robinson Jun 30 '14 at 15:25
  • 1
    im having the same issue, my label and imageview is returning nil from a custom collection cell. I've check the connects and class name is correct but no luck. any solution everyone? – Arnlee Vizcayno Jul 04 '14 at 05:27
  • 1
    This is so simple but I cannot get it to work either. Swift is giving the the SWIFTS ! – DogCoffee Aug 18 '14 at 13:20

1 Answers1

10

Found the answer here

Remove, self.collectionView.registerClass(MyCustomCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

Read link for detailed reason why

Community
  • 1
  • 1
DogCoffee
  • 18,799
  • 9
  • 81
  • 114