10

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?

2
  • 2
    You don't need call self.tableView.registerClass(SegmentCell.self, forCellReuseIdentifier: "Cell") on viewDidLoad, just set the prototypeCells class to "SegmentCell" on Mainstoryboard. Commented Oct 22, 2015 at 18:25
  • 1
    I realise this is a really old question, but it currently has no answer and I happened upon it because I'm encountering a similar problem. In your case, I think the weak var is the key - if your SegmentCell is the only thing that ever holds a reference to the UILabel, that won't contribute to the retain count and so the label will be deallocated because nobody has a strong reference to it. By the time you attempt to set .text, it's already gone. Commented Apr 23, 2019 at 11:43

6 Answers 6

11

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.

Sign up to request clarification or add additional context in comments.

Comments

3

Based on the way you register your cell, it is not going to be loading it from a storyboard or xib file. It will be invoking that init method only. Your init method does not create the label, so it will always be nil.

You should also use dequeueReusableCellWithIdentifier(_:forIndexPath:) instead of dequeueReusableCellWithIdentifier(_:). The latter predates storyboards and will return nil unless you have previously created a cell with that identifier and returned it.

Lastly, the init method that the tableView is calling is not the one you've implemented, or the app would crash on test.text = ... while trying to unwrap a nil optional.

11 Comments

Im creating it in the storyboard, in the prototype cell.
Then you should not register it with the tableView in code, and you can expect init(coder:) to get called.
So I've removed the tableView.registerClass and changed the init method in the SegmentCell class to required init?(coder aDecoder: NSCoder) { still having the same issue
You should also use dequeueReusableCellWithIdentifier(_:forIndexPath:) instead of dequeueReusableCellWithIdentifier(_:). The latter predates storyboards and will return nil unless you have previously created a cell with that identifier and returned it.
You do not need one. You can set that text in the storyboard.
|
1

UITableViewCell class and ContentView class can't be same.

Comments

0

You should register it first in the viewDidLoad function.

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

1 Comment

This is actually on the right track. But the call is this: TableView.register(UINib.init(nibName: "CellXibName", bundle: nil), forCellReuseIdentifier: "CellIdentifier")
0

I have same issue, after add custom cell class to cell in tableview placed in Storyboard. I also register cell, like in documentation. But now I solve my issue.

"I remove registering and inspect cell again, all IBOutlets initialised"

Comments

0

I think that basically the outlets are not setup yet at init time. If you want to get to the outlets and manipulate them, then override didMoveToSuperview or similar and do it in there. For instance, this is what I had to do to get to the button outlet in my cell:

open class MyTableViewCell: UITableViewCell { // Top stack view, not the inner one. @IBOutlet weak var stackView: UIStackView! @IBOutlet weak var buttonView: UIButton? open override func didMoveToSuperview() { buttonView?.titleLabel?.adjustsFontForContentSizeCategory = true } func adjustForAccessibilityCategory(accessible: Bool) { if accessible { stackView.axis = .vertical stackView.alignment = .leading stackView.spacing = 2 } else { stackView.axis = .horizontal stackView.alignment = .center stackView.spacing = 20 } } open override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { if #available(iOS 11.0, *) { adjustForAccessibilityCategory(accessible: traitCollection.preferredContentSizeCategory.isAccessibilityCategory) } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.