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)
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)
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.
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.
you should get the cell like this:
var cell = tableView.dequeueReusableCellWithIdentifier("reuseCell", forIndexPath: indexPath) as bookCell
You need to provide the indexPath param.
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.