0

Does anybody know the reason for this error that occurred when setting the text property for UILabel in my custom UITableViewCell in the cellForRowAtIndexPath method?

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

enter image description here

Rob
  • 371,891
  • 67
  • 713
  • 902
Mbusi Hlatshwayo
  • 504
  • 10
  • 23
  • possible duplicate of [Fatal error: unexpectedly found nil while unwrapping an Optional values](http://stackoverflow.com/questions/24643522/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-values) – GoZoner Mar 22 '15 at 03:12
  • We need to see `bookCell`, but you undoubtedly have neglected to hook up the `@IBOutlet` to `bookPoster`. (Unrelated, but naming conventions suggests that class names should start with uppercase letters, e.g. `BookCell` rather than `bookCell`.) – Rob Mar 22 '15 at 03:19

4 Answers4

1

Looks to me like you're trying to access a UILabel on the 'bookCell' UITableViewCell directly & set its text. UILabels in this scenario are usually Optionals - so you need to first unwrap the optional (to make sure its value is actually present - i.e. non-nil).

Give this a try and let me know if it helps!

if let b = cell.bookPoster {
  b.text = "Hello World"
}

If that fixes the error, I recommend reading up on Optionals.

gemmakbarlow
  • 812
  • 12
  • 21
0

You either have to declare the cell in a NIB and load the NIB in your code and load the cell from that, or when not using a NIB, when you dequeue the cell the first time, you have to create all the subviews and generally add them to the content view of the cell. THEN you can access the cell's contents, but each cell dequed has to be tested for newness initialized from somewhere first.

Simply casting a dequeued cell into your subclass type is insufficient.

clearlight
  • 10,772
  • 11
  • 48
  • 65
  • 1
    No, this is not true. You can use cell prototypes in your storyboard and it saves you from having to use either of the approaches suggested here. You _can_ use this NIB pattern if you want (e.g. if you're going to be reusing the same cell in lots of different tables, it's a good pattern), but you don't have to. And the second approach, manually adding instantiating the cell class if dequeue failed and programmatically adding the subviews is a fairly dated technique and you don't see that very often anymore. – Rob Mar 22 '15 at 03:22
  • 1
    Thanks Rob. I forgot about cell prototypes - one aspect of Table View programming I haven't tried yet and meant to get back to. Didn't mean to mislead anyone, and thanks or setting the record straight – clearlight Mar 22 '15 at 03:26
0

you should get the cell like this:

var cell =  tableView.dequeueReusableCellWithIdentifier("reuseCell", forIndexPath: indexPath) as bookCell

You need to provide the indexPath param.

Dario
  • 2,977
  • 1
  • 14
  • 16
  • 1
    No you don't. This has nothing to do with the problem. It's best practice, yes, but unrelated to the problem here. – Rob Mar 22 '15 at 03:20
0

My guess is that you have not registered the cell's nib for the tableView. In your viewDidLoad method, you should do something like this:

myTableView.register(UINib.init(nibName: "myCellNibName"), bundle: nil), forCellReuseIdentifier: "reuseCell") 

This way your nib will be loaded and ready for use.

dvp.petrov
  • 1,028
  • 13
  • 20