Chris G.
Chris G.

Reputation: 25974

Unable to load xib file

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?

Upvotes: 0

Views: 333

Answers (2)

Chris G.
Chris G.

Reputation: 25974

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

Upvotes: 0

0ndre_
0ndre_

Reputation: 3631

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..

Upvotes: 1

Related Questions