0

The following code is a cleansed & rehashed version of a previous post.
ref: This class is not key value coding-compliant for the key...why?

import Foundation
import UIKit

var x = 1

struct DiaryItem {
    var title:String?
    var subTitle:String?
    var leftImage:UIImage?
    var rightImage:UIImage?
    init(title:String, subTitle:String) {
        self.title = title
        self.subTitle = subTitle
    }
}

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


class DiaryTableViewController: UITableViewController {
    let kCellIdentifier = "DiaryCell"
    var diaryCell:DiaryTableViewCell?
    var objects = NSMutableArray()  //...global var.

    override func viewDidLoad() {
        self.title = "My Diary"
        tableView.registerClass(DiaryTableViewCell.self, forCellReuseIdentifier: kCellIdentifier)
    }

    // -----------------------------------------------------------------------------------------------------
    // MARK: - UITableViewDelegate

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let controller = gStoryboard.instantiateViewControllerWithIdentifier("DiaryPlayerVC") as DiaryPlayerViewController
        self.navigationController?.pushViewController(controller, animated: true)
    }

    // -----------------------------------------------------------------------------------------------------
    // MARK: - UITableViewDataSource

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    // -----------------------------------------------------------------------------------------------------
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

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

        let cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as DiaryTableViewCell?
        // cell?.selectionStyle = .None

        println("\(x++)) Inside cell")
        cell!.TitleLabel?.text = "Hello"
        cell!.TitleLabel?.backgroundColor = UIColor.blueColor()
        cell!.SubTitleLabel?.text = "World"
        cell!.contentView.backgroundColor = UIColor.redColor()

        return cell!
    }  
...    
}

I'm getting the cell, but no elements of that cell.

1) Inside cell
(lldb) po cell!.TitleLabel
nil

enter image description here

enter image description here

enter image description here

enter image description here

I cleaned up the code, it compiles & runs okay. The cell is loaded and painted with red so I can see it was loaded. But none of the cell's contents are instantiated.

why?

It's seeing the members of the cell now...
But now I'm getting:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7f9691594810> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key leftImageView.'

If I disconnect the outlets, I get the images:

enter image description here

I've added the required init() but still have the same problem:

class DiaryTableViewCell: UITableViewCell {
    @IBOutlet weak var leftImageView: UIImageView!
    @IBOutlet weak var rightImageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var subTitleLabel: UILabel!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        NSLog("init coder")
    }
}
Community
  • 1
  • 1
Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105

3 Answers3

0

You have declared your properties as implicitly unwrapped optionals, signing that your are sure they are not nil. Thus there is no need to use optional chaining.

And please use lower case property names because upper case names are used for class names:

class DiaryTableViewCell: UITableViewCell {
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var subTitleLabel: UILabel!
    @IBOutlet weak var leftImageView: UIImageView!
    @IBOutlet weak var rightImageView: UIImageView!
}

Then try this:

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

    if let cell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as? DiaryTableViewCell {
        // cell.selectionStyle = .None

        println("\(x++)) Inside cell")
        cell.titleLabel.text = "Hello"
        cell.titleLabel.backgroundColor = UIColor.blueColor()
        cell.subTitleLabel.text = "World"
        cell.contentView.backgroundColor = UIColor.redColor()

        return cell
    }

    return UITableViewCell()
}  

If that crashes there must be something wrong with your outlet bindings.

Did you set your DiaryTableViewCell as the class for the nib in InterfaceBuilder?

zisoft
  • 22,770
  • 10
  • 62
  • 73
  • "return nil" --> Type 'UITableViewCell' does not conform to protocol 'NilLiteralConvertible' Yes: My DiaryTableViewCell is set as the class for the nib. – Frederick C. Lee Nov 04 '14 at 12:40
  • Ok, removed the `nil` from my sample code. Does that method still crash? – zisoft Nov 04 '14 at 12:43
  • I had to revert to my original code, with the revised lower camel-case of the outlets. The func() requires a cell value for its return. Still no instantiated elements of cell. – Frederick C. Lee Nov 04 '14 at 12:50
  • Your class is in a nib? Then you have to call `registerNib(...)`. Remove the `registerClass(...)` – zisoft Nov 04 '14 at 12:55
  • I've revised the code accordingly (see above). But now I'm getting the runtime error: *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key leftImageView.' – Frederick C. Lee Nov 04 '14 at 13:11
  • Your tableCellClass lacks the init methods. – zisoft Nov 04 '14 at 13:17
  • posted a new answer. I will delete this answer soon since it wasn't useful. – zisoft Nov 04 '14 at 13:26
  • Doesn't a Swift class's init() modus operandi differ from ObjC? I don't believe I need to alloc a Swift class. Doesn't the IB take care of instantiating the Swift class? I think we're getting close; but still lost. ... need code sample of a Swift custom-cell class. – Frederick C. Lee Nov 04 '14 at 13:34
  • try the code in my other answer and see if it works. `awakeFromNib()` – zisoft Nov 04 '14 at 13:35
  • okay... done that + init(). No change. The moderator says our discussion is too long. So I'm going to study this further tomorrow. Thanks for your suggestions. – Frederick C. Lee Nov 04 '14 at 14:06
0

Your tableCellClass lacks the awakeFromNib()method for nib:

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

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}
zisoft
  • 22,770
  • 10
  • 62
  • 73
  • I found the solution. My Answer is at my original post: http://stackoverflow.com/questions/26729479/this-class-is-not-key-value-coding-compliant-for-the-key-why/26743545#26743545 – Frederick C. Lee Nov 04 '14 at 19:40
0

I found the solution:
This class is not key value coding-compliant for the key...why?

Here's the repost:

I had linked the UI elements to the WRONG source:

enter image description here

Here's the result:
enter image description here

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