0

I have linked the UITextView to my custom cell class and when I call #dequeueReusableCell#, I assign it to constant cell as my CustomCellClass.

I cannot quite figure out why this happens. When I checked the variables section next to the log console, it says everything is nil.

All the values are nil: enter image description here

However when I open up theTableViewCell, I found "_imageView" and "_textLabel" and if I assign my values to these, it shows up. Even then, I cannot find UITextView there.

Alternate values enter image description here

Thanks for any help.

Code for custom Cell Class:

class PostCell: UITableViewCell { @IBOutlet weak var postImageView: UIImageView! @IBOutlet weak var postTextView: UITextView! @IBOutlet weak var postName: UILabel! } 

Function where I assign the values:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier , for: indexPath) as! PostCell let cellPost = self.posts[indexPath.row] cell.textLabel?.text = "Hola" cell.postName.text = cellPost._text cell.imageView?.image = UIImage(named: "UV_Logo-12") return cell } 

In the above code, textLabel and imageView are not the var names in the custom class.

viewDidLoad Function:

@IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. queryPosts() tableView.delegate = self tableView.dataSource = self self.tableView.register(PostCell.self, forCellReuseIdentifier: cellReuseIdentifier) } 
4
  • 3
    Is the class of the cell set to PostCell in Interface Builder? Are the outlets connected? Commented Aug 8, 2018 at 18:40
  • @vadian Yes, class is set to PostCell and the outlets are connected Commented Aug 8, 2018 at 20:01
  • What's the cellReuseIdentifier? Is it the same in IB and code? Commented Aug 8, 2018 at 22:04
  • Yes the cellReuseIdentifier is same in IB and code. I don't know what else to check Commented Aug 9, 2018 at 4:00

3 Answers 3

6

Two issues:

  1. If the cell is designed as prototype cell in Interface Builder and you don't use an extra XIB you must not register the cell.
  2. If queryPosts() is supposed to populate the data source array you have to set delegate and datasource before calling the method.
    The best way is to connect both in Interface Builder and delete the two lines.

override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self queryPosts() } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, really appreciate it. I connected delegate and datasource in Interface Builder, thanks for the suggestion. It worked when I didn't register the cell, but I don't quite understand why that worked.
The framework registers the cell implicitly when creating the prototype cells. Overriding obviously breaks the functionality.
0

The behaviour you're describing is normal. textLabel and imageView are built-in properties of every UITableView cell. They are nil by default (that is why they are Optionals). If you refer to them, though, they come into existence, they are no longer nil, and the corresponding views appear. If you don't, they don't.

So the rule is, either user a built-in style or use a custom style, and for a custom style, don't refer to those properties.

6 Comments

Matt, I think the question is a little confusing. The code shows using textLabel because they have found that it isn’t nil while the properties of their custom cell class are nil which would indicate their is a problem with their storyboard/nib
@Paulw11 I agree but my point is the non-nil-ness of textLabel and imageView proves nothing; all three of his custom cell's properties are still nil which is all that matters.
Thank you for clarifying my misconceptions about the built in properties. But the problem is I don't know why my custom cell properties are nil.
It’s because of how you are getting the cell. But you see, you have not shown us that code. You are probably registering the PostCell class with the table view for the cell reuse identifier. And that is your mistake. But you did not show that code so it is hard to help.
I added the viewDidLoad function. What is the mistake that I am doing ?
|
0

I faced the same issue while assigning value to a label which was the part of tableview.

Error:Unexpectedly found nil while unwrapping an Optional value. Occurs when I assign a value to Label which is part of UITableViewCell

firstly I tried to register my cell with table view using below code, but it did not work for me

self.tableView.register(MyCustomCell.self, forCellReuseIdentifier: cellReuseIdentifier) 

Registering my cell with above code does not worked for me.

Then, I tried registering my TableViewCell with register with NIB name and it worked for me.

So I registered the table view cell in viewdidload() using its NIB name which worked for me

override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. //register your table view cell in Viewdidload() using Nib Name tableView.register(UINib.init(nibName: "MyCustomCell", bundle: nil), forCellReuseIdentifier: "myCustomCell") } 

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.