0

I am trying to use a custom UITableViewCell.
Setup: Xcode 7.1, Swift, running in Emulator
Steps:
1. Created a new "Cocoa Touch Class"(CMD+N) with Subclass of UITableViewCell
2. Also create XIB file, when creating the swift file.
3. Now I would like to load the XIB file into a TableViewController

 override func viewDidLoad() { super.viewDidLoad() let yourNibName = UINib(nibName: "MyTableViewCell", bundle: nil) tableView.registerNib(yourNibName, forCellReuseIdentifier: "test") // << EXC_BAD_INSTRUCTION ... 

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

Info:
1. I can see in the debug area: that yourNibName has a memory address but all other properties is 0x0. Therefore the XIB must not be loaded?
2. I can see it is a member in the Inspector: Target Membership
3. It is in "Build Phases/Copy Bundle Resources"
4. I do Product Clean every each thing I am trying.
But why?

4
  • Did you add your Nib to your project's target? Commented Jan 20, 2016 at 16:14
  • Yes: I can see it is a member in the Inspector: Target Membership. Commented Jan 20, 2016 at 16:15
  • Did you add your "MyTableViewCell" identifier to the prototype cell? Commented Jan 20, 2016 at 16:20
  • In the Attribute Inspector I have set; identifier to "test". Is that what you mean? Commented Jan 20, 2016 at 16:23

2 Answers 2

1

try this

your top should look like this

class myTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

view did load func like this:

 tableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "test") tableView.delegate = self tableView.dataSource = self 

and this func like this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("test", forIndexPath: indexPath) as! MyTableViewCell tableView.rowHeight = 470 tableView.allowsSelection = false return cell } 

If it does not work let me know..

Sign up to request clarification or add additional context in comments.

3 Comments

in MyTableViewCell.xib your cell identifier should be "test" so check if it is the same and you should also check if there is no spelling mistake in "MyTableViewCell"
Thanks Ondra, I created the ViewController from code and was missing the self.view itself :-/
Yes, you and others was/is on the right track :-) See my answer. Thanks
0

One could only wish for better error messages in Xcode. That said I was creating a TableViewController from code, and was missing the view itself.

self.tableView = UITableView(frame:self.view.frame) 

And ofcource you can not do anything with the view being nil, and that includes trying to tableView.registerNib

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.