-3

I'm working with nib files for the first time and trying to add a tableview with a tableview cell. I created a nib file of type UIView controller, then I dragged the tableview onto the view, and in the viewcontroller.swift I added all of the necessary delegate, datasource, cellForRowatIndexPath, numberOfRowsinSection, etc, just like normal. But the app crashes on loading. I have looked at several other questions, notably these:

Custom UITableViewCell from nib in Swift

Can't make UiTableView work

But those solutions did not work for me completely and it still crashes on loading. Another error message I've gotten has been "this class is not key value compliant."

So, what are the exact steps to make a uitableview in a nib file? From what I understand:

  1. File--> New-->File-->Cocoa Touch Class-->Subclass UITableViewController. this sets up the table view. we will call this View1.swift

  2. File-->New-->File-->Cocoa Touch Class-->Subclass UITableViewCell. this sets up the cell in the tableview. we'll call this View1TableCell.swift

  3. in View1.swift, register the nib:

     tableView.registerNib(UINib(nibName: "View1", bundle: nil), forCellReuseIdentifier: "View1CellID")
    
  4. Give the cell a reuse identifier. We will say this is "View1CellID"

  5. in View1.swift, in cellforRowAtIndexPath, dequeue the cell with the correct cell identifier.

So, all these steps should work so that I can add any label or button to my View1TableCell nib, and those changes will be seen on the tableview when I build and run, correct? What am I doing wrong?

Community
  • 1
  • 1
jjjjjjjj
  • 4,203
  • 11
  • 53
  • 72

2 Answers2

1

The nib you register should be the one containing the cell, not the view controller. From Apple docs:

Parameters

A nib object that specifies the nib file to use to create the cell. This parameter cannot be nil.

Community
  • 1
  • 1
Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • this helps. I changed it and now I receive no error and the tableview loads, but my custom cell didn't appear, it was just a blank tableview. – jjjjjjjj Sep 09 '15 at 13:07
  • Is `cellforRowAtIndexPath` actually being called (set breakpoint)? If so, what's the code there look like? – Phillip Mills Sep 09 '15 at 14:03
0

So the view that holds your table is View1, from your explanation.

If you want to implement a custom cell, you need to create a new class, to extend UITableViewCell. That class should also have a nib

So create a new class

class MyCell: UITableViewCell {

   override func awakeFromNib() {
        super.awakeFromNib()
        setupCell()
    }

    func setupCell() {
        //setup your ui here
    }
}

Now in View1.swift, in viewDidLoad, register the nib

func viewDidLoad() {
     super.viewDidLoad()
     //register the nib of the cell for your cell
     tableView.registerNib(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "MyCellIdentifier")
}

now use the cell in your tableview

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("MyCellIdentifier") as! MyCell
       //set the stuff you need in your cell
       return cell 
}
trusk
  • 1,634
  • 2
  • 18
  • 32