简体   繁体   中英

Load UITableViewCell from xib file in a UITableView in a view

I have a view (not a view controller) that pop up when I press a button.

In this view, I have a UIButton and a UITableView . I want to use the UITableViewCell I designed in a xib file.

So I created my cell class:

class PickerViewCell: UITableViewCell {

    @IBOutlet weak var flagImageView: UIImageView!
    @IBOutlet weak var codeLabel: UILabel!
    @IBOutlet weak var nameLabel: UILabel!

    func setCell(myObject: myObject) {
        flagImageView.image = UIImage(named: myObject.code)
        codeLabel.text = myObject.code
        codeLabel.font = myObject.kPickerViewCodeFont
        nameLabel.text = myObject.name
        nameLabel.font = myObject.kPickerViewNameFont
    }
}

My xib file:

在此处输入图片说明

Here I didn't set the owner of the xib (I tried with as well), I put the custom class for the UITableViewCell and no reuse identifier (I tried with as well).

In the init function of my view (the one containing the table view), I do the following:

let nib = UINib(nibName: "PickerViewCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")

And finally I implement the method:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! PickerViewCell

    let myObject = myObjects[indexPath.row]
    cell.setCell(myObject)

    return cell
}

When I want to show (create) my view, the debugger stop at

let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! PickerViewCell

and I get the following error:

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

What's wrong?

EDIT:

在此处输入图片说明

Check your connection of @IBOutlet weak var codeLabel: UILabel! with IB in PickerViewCell . Try to reconnect it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM