Ronald Prithiv
Ronald Prithiv

Reputation: 3

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

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)
}

Upvotes: 0

Views: 2533

Answers (3)

ABS
ABS

Reputation: 7732

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")
}

Upvotes: 0

vadian
vadian

Reputation: 285069

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()
}

Upvotes: 6

matt
matt

Reputation: 534950

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.

Upvotes: 0

Related Questions