-1

Why am I getting a 'nil' error/UILabel during my second pass thru the table cell listing iteration?

1) Inside cell
2) Inside cell
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) po cell?.contentView.viewWithTag(TitleLabelTag)
nil

enter image description here

enter image description here

Here I link the elements in the code; and register the cell:

class DiaryTableViewCell: UITableViewCell {
    @IBOutlet weak var TitleLabel: UILabel!
    @IBOutlet weak var SubTitleLabel: UILabel!
    @IBOutlet weak var leftImageView: UIImageView!
    @IBOutlet weak var rightImageView: UIImageView!
}


class DiaryTableViewController: UITableViewController {
    let kCellIdentifier = "DiaryCell"
    var cellNib:UINib?
    var diaryCell:DiaryTableViewCell?
    var objects = NSMutableArray()  //...global var.

    override func viewDidLoad() {
        self.title = "My Diary"
        cellNib = UINib(nibName: "TableViewCells", bundle: nil)
        tableView.registerClass(DiaryTableViewCell.self, forCellReuseIdentifier: kCellIdentifier)
    }
    ...

Yet I'm getting the runtime error here:

enter image description here

Here's what I get in the console:

1) Inside cell
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) po cell!.TitleLabel
nil

What's missing here?

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • The view probably gets loaded from the nib file with a retain count of 1, and then it gets autoreleased. Since nothing owns it, it becomes 0 shortly after and is set to nil. Note that viewWithTag() is documented as being able to return nil, but your code forces the compiler to drop that and treat it as something that cannot ever return nil. This is a bad idea. – Abhi Beckert Nov 04 '14 at 03:52

3 Answers3

0

It's a pretty bad idea to select a view with a tag. It's a much better idea to subclass your UITableViewCell and give it a property to access the elements.

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
0

If you are creating static cells and loading you need to create an IBoutlet for them in your .h correctly.

Moreover remove line

tableView.registerClass(...) statement from your code. Look at this link might help and is very similar except its for collectionview. -

Why is UICollectionViewCell's outlet nil?

Community
  • 1
  • 1
Aniket Bochare
  • 427
  • 2
  • 10
  • I re-wrote the code/paradigm to use outlets vs tagged views. But now I'm getting a runtime KVO complaint: http://stackoverflow.com/questions/26729479/this-class-is-not-key-value-coding-compliant-for-the-key-why – Frederick C. Lee Nov 04 '14 at 07:21
0

1) I moved the cell registration to the viewDidLoad().
2) I forgot to place the '?' after the TitleLabel & SubTitleLabel; to notify the compiler that these labels could be nil.

enter image description here

I don't see the altered cell yet (empty rows); but I'm not getting runtime errors.

Unfortunately I merely cured the symptom; not the cause. I'm still getting nil UILabels.
...working on revision and cleaner code.

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105