0

I've created custom cell and connected it as:

let cell: statisticsCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! statisticsCell

and later I'm using it as:

cell.calendarLabel.text = recordsArray[indexPath.row].time
cell.dayLabel.text = "A \(recordsArray[indexPath.row].date)"
cell.amountLabel.text = "\(recordsArray[indexPath.row].count)"

but when I run my app, just this cell from those 3 - returns nil and crashes my app:

cell.dayLabel.text = "A \(recordsArray[indexPath.row].date)"

the value is not nil, I checked it.

Can anyone say me why it returns nil? Why just one label from 3 returns nil, while it's not nil?

Crash

It crashes with:

fatal error: unexpectedly found nil while unwrapping an Optional value

on the

cell.dayLabel.text
John Doe
  • 769
  • 3
  • 10
  • 24

3 Answers3

0

See if you have created outlets properly for dayLabel and connected to the element in Storyboard.

Amulya
  • 162
  • 1
  • 11
  • I've connected it like those, other 2 labels. – John Doe Feb 11 '16 at 06:56
  • try to redo once again . Bcz if the reference is not referring to an object on the UI and you try to change the properties, It will also give similar errors . So better try to disconnect and reconnect and lets see. – Amulya Feb 11 '16 at 07:04
  • have you configured your custom class to the custom cell properly & try to see if the cell object is allocated – Amulya Feb 11 '16 at 07:08
  • yes! Other 2 labels from the exact cell works perfectly! Just one does not! – John Doe Feb 11 '16 at 07:10
0
cell.calendarLabel?.text = recordsArray[indexPath.row].time
cell.dayLabel?.text = "A \(recordsArray[indexPath.row].date)"
cell.amountLabel?.text = "\(recordsArray[indexPath.row].count)"

As mentioned in this answer you can try this too:-

if let label = cell.dayLabel{
    label.text =  "A \(recordsArray[indexPath.row].date)"
}
Community
  • 1
  • 1
Vizllx
  • 8,843
  • 1
  • 36
  • 77
0

How did you check if the value is not nil? Make sure by logging in the console, right before you set it to text property of dayLabel, like this:

print(recordsArray[indexPath.row].date)

Also, the date - is it of String or NSDate type?

Antonin Charvat
  • 869
  • 7
  • 16