Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UITableviewCell IBOutlet always nil

I have a custom UITableViewCell subclass with a simple IBOutlet setup for a UILabel.

class SegmentCell: UITableViewCell {

    @IBOutlet weak var test: UILabel!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        test.text = "Some Text"
    }

    required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
    }

}

Convinced I have everything set up correct have followed other answers, but the UILabel is always nil.

ViewController: viewDidLoad:

self.tableView.registerClass(SegmentCell.self, forCellReuseIdentifier: "Cell")

cellForForAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! SegmentCell
    return cell
}
  • Cell is set to Custom
  • Reuse identifier is correct
  • Cells class is SegmentCell
  • Tableview content is Dynamic Prototypes

What am I missing?

like image 736
Kyle Goslan Avatar asked Oct 22 '15 18:10

Kyle Goslan


1 Answers

Since You are uisg Xib file you have to register with ,

tableView.register(UINib(nibName: "yourNib", bundle: nil), forCellReuseIdentifier: "CellFromNib")

Think, if only register with class ,system will not know the its Xib.This work for me.

like image 75
Yodagama Avatar answered Sep 29 '22 23:09

Yodagama