4

I'm writing an app in which I have a horizontal collection view within a table view cell.

When trying to set the text in the cell programmatically, the app crashes with the fatal error: unexpectedly found nil while unwrapping an Optional value error message.

To me, and to a much more experienced cocoa-touch programmer, this suggested that my IBOutlets were not connected correctly, and so cell.filmTitle would return nil when explicitly unwrapped. So I unconnected all my IBOutlets to the cell XIB, and removed all the code behind them, before recoding the IBOutlets and reconnecting them. But the error was still there.

The code for the custom cell is:

class homeWhatsOnCellectionCell: UICollectionViewCell {
    @IBOutlet var filmTitle: UILabel!
    @IBOutlet var filmPoster: UIImageView!
}

And the code for the collection view datasource is:

func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
    var cell: homeWhatsOnCellectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("Home Whats On Collection View Cell", forIndexPath: indexPath) as homeWhatsOnCellectionCell
    cell.filmTitle.text = "The Hunger Games: Catching Fire"
    cell.filmPoster.image = UIImage(named: "The Hunger Games.jpg")
    return cell
}

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

Edit: From a conversation with Nate Cook, I can say that filmTitle is the cause of the problem, with it being nil despite being connected properly to a label. Also, dump(cell) outputted "-" to the console. Thanks to anybody who can help.

Edit 2: Through further investigation, I have found that the cell is not being seen at all. When I removed the IBOutlets, and tried to simply show 10 cells as they appear in the XIB, they don't show up at all, and all I get is ten blank cells of the same size. It is showing some cells, as the scrollbar changes size and scrolls based on how many cells are being displayed.

Cailean Wilkinson
  • 1,420
  • 2
  • 19
  • 32
  • What's the value of `cell` after dequeueing it? Is `"Home Whats On Collection View Cell"` really the reuse identifier? – Nate Cook Jul 24 '14 at 16:29
  • @Nate How do I find the value of `cell`? Running `println(cell)`, if thats what you mean, prints: `_TtC21Films225homeWhatsOnCellectionCell: 0x797d6700; baseClass = UICollectionViewCell; frame = (0 10; 104 160); layer = >` – Cailean Wilkinson Jul 24 '14 at 16:42
  • No worries. Can you do the same for `cell.filmTitle` too? – Nate Cook Jul 24 '14 at 16:44
  • @Nate That came up as `nil`, so `cell.filmTitle` is the problem. Does that suggest anything to you? – Cailean Wilkinson Jul 24 '14 at 16:46
  • 1
    Either that the `IBOutlet` must not be hooked up correctly, or there's a bug in IB that is giving you trouble. Just to verify, you only have one prototype cell with type `homeWhatsOnCellectionCell`, it has re-use identifier "Home Whats On Collection View Cell", and the outlets are hooked up to the fields in that cell? – Nate Cook Jul 24 '14 at 16:55
  • @Nate Yeah, I only have one cell XIB with that class, yeah its identifier is "Home Whats On Collection View Cell", and all the outlets are hooked up. – Cailean Wilkinson Jul 24 '14 at 16:58
  • @Nate it may be worth my mentioning that if I comment out the `cell.filmTitle.text` line, the `cell.filmPoster.image` line produces the same error – Cailean Wilkinson Jul 24 '14 at 16:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57937/discussion-between-nate-cook-and-hyperion). – Nate Cook Jul 24 '14 at 17:05
  • Having the same issue. The cell is a valid object, however none of the IBOutlets are valid. – Cliff Helsel Aug 28 '14 at 19:11
  • 1
    The issue I had related to [this question](http://stackoverflow.com/a/25166762/320471). I had to **remove** `contactsView.registerClass(ContactCell.self, forCellWithReuseIdentifier: "ContactCell")` – hayesgm Sep 14 '14 at 23:37
  • If you are using a Xib try: ` self.yourTableView.registerNib(UINib(nibName: "myCellName", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "myCellIdentifier")` ...Also check that your cell hast the correct name of the class and check if you assign and change the name of and old IBOUtlet and its still hooked up, it appears with an ! mark – Pach Oct 02 '14 at 15:26
  • Hard to tell what's wrong. At the very least you need to post a stack trace so we know exactly where this error occurs. – n13 Jul 21 '15 at 09:13

1 Answers1

0

Sounds like the XIB file doesn't even get loaded.

Either set this up correctly in IB, or manually in your view controller in viewDidLoad do this

// register table view cell in external XIB file "YourFileName.xib"
tableView.registerNib(UINib(nibName: "YourFileName", bundle: nil), forCellReuseIdentifier: "Home Whats On Collection View Cell")

If you're using a storyboard with prototype cells, make sure to set the cell reuse identifier in Interface Builder.

I am guessing that the core of the issue is that the table view doesn't know which class to load with this reuse identifier. Would be nice to have a stack trace and a whole lot more information about this though. Are you using a XIB file with a table view cell? Are you using prototypes in a storyboard?

n13
  • 6,843
  • 53
  • 40