4

I've linked output from the IB to the code, as shown below.

class DiaryTableViewCell: UITableViewCell {
    @IBOutlet weak var TitleLabel: UILabel!
    @IBOutlet weak var SubTitleLabel: UILabel!
    @IBOutlet weak var leftImageView: UIImageView!
    @IBOutlet weak var rightImageView: UIImageView!
}

Here, I'm registering the class:

override func viewDidLoad() {
   self.title = "My Diary"
   cellNib = UINib(nibName: "TableViewCells", bundle: nil)
   tableView.registerClass(DiaryTableViewCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)
}

enter image description here

enter image description here

But I keep getting the following runtime error:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '...setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key SubTitleLabel.'

From within the following code:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as DiaryTableViewCell?

if (cell == nil) {
    tableView.registerClass(DiaryTableViewCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)
    cell = cellNib?.instantiateWithOwner(self, options: nil)[0] as? DiaryTableViewCell
                cell?.selectionStyle = .None
}

if (cell != nil) {
    println("\(x++)) Inside cell")
    cell!.TitleLabel.text = "Hello"
    cell!.SubTitleLabel.text = "World"
}

return cell!
}

Specifically, it's happening here:

cell = cellNib?.instantiateWithOwner(self, options: nil)[0] as? DiaryTableViewCell

Question: How am I violating the key value coding-compliant for a UILabel?

This hasn't happened before... UILabel is KVO compliant.

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • 1
    What is `cellNib?`. And why don't you call `registerClass` in `viewDidLoad` so that `tableView.dequeueReusableCellWithIdentifier` always returns the correct cell? – Martin R Nov 04 '14 at 07:15
  • Have you tied this "DiaryTableViewCell" class to your TableViewCell in your identity inspector under custom class? – Aniket Bochare Nov 04 '14 at 07:30
  • possible duplicate of [What does this mean? "'NSUnknownKeyException', reason: ... This class is not key value coding-compliant for the key X"](http://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key) – jtbandes Aug 03 '15 at 06:54
  • Issue not reproducible (see OP's own [answer](http://stackoverflow.com/a/26743545/2227743)). – Eric Aya May 31 '16 at 12:52

5 Answers5

9

I linked to the WRONG Source!

enter image description here

Here's the result:

enter image description here

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
1

You should not be calling instantiateWithOwner yourself inside tableView:cellForRowAtIndexPath.

Register the nib in viewDidLoad and then dequeueReusableCellWithIdentifier will do all the work for you.

The reason for your particular error is that you are calling instantiateWithOwner passing self as the owner and so the nib is trying to wire the outlets up to your UITableViewDataSource implementation class rather than a DiaryTableViewCell.

Mike Pollard
  • 10,195
  • 2
  • 37
  • 46
  • I created a revised question with the suggested change of code but remaining problem of nil cell contents: http://stackoverflow.com/questions/26733848/i-got-the-uitableviewcell-but-cells-elements-are-nil-why – Frederick C. Lee Nov 04 '14 at 11:22
1

Show the references of your ViewController rigth-clicking in it on the Document Outline. Probably you will see a warning in one of the references. Delete it and link it again if still need it.

enter image description here

Viker
  • 3,183
  • 2
  • 25
  • 25
0

sometimes its like when you create button in your xib, you create one button and copy paste other buttons from one buttons, in that case this error occurs, and yes removed connections from xib could also be an reason.

Nitin
  • 451
  • 5
  • 17
0

I have created a TableViewCell same like your & have the same problem. I have research solving the problem but nothing. Then I delete Label in TableViewCell and recreate again, connect it to TableViewCell through File Owner,v..v And the result is right. No error. You should recreate them again.

AmyNguyen
  • 437
  • 6
  • 10