0

I am creating a table, and using this code to generate the cells:

let cell2:iPRoutineSpecificCell = self.routineSpecificsTable.dequeueReusableCell(withIdentifier: "Cell2") as! iPRoutineSpecificCell

The cell contains a label. When i say

cell2.label.text = "Test"
return cell2

I get the error:

fatal error: unexpectedly found nil while unwrapping an Optional value

I have this exact arrangement elsewhere in the app and it is working fine. Why is it not working this time? The only difference between the two views is that this one contains two tables. However, if i use:

cell2.textLabel?.text = "Test"

Then it works.

Any help appreciated!

ZiEiTiA
  • 275
  • 2
  • 16
  • can you show define `iPRoutineSpecificCell`? – Adrian Bobrowski Feb 19 '17 at 14:13
  • 1
    You need to connect your label with iPRoutineSpecificCell – DiegoQ Feb 19 '17 at 14:16
  • @DiegoQ I have connected the label (i think) - you mean the alt+drag and connect as outlet to the file? – ZiEiTiA Feb 19 '17 at 14:18
  • @ZiEiTiA yes, that's what I mean. – DiegoQ Feb 19 '17 at 14:22
  • 1
    @DiegoQ - I have done this. – ZiEiTiA Feb 19 '17 at 14:25
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) –  Feb 19 '17 at 14:27
  • Deleted the debugging comments here, as the issue seemed to be resolved. – Bhargav Rao Feb 19 '17 at 17:57
  • As long as you're not supporting anything below iOS 6, you should use the `dequeueReusableCell(withIdentifier:for:)` method. This method takes in an additional parameter (IndexPath) and is guarenteed to return a cell (as opposed to a `UITableViewCell?`) – Taylor M Feb 19 '17 at 18:06

3 Answers3

3

Please remove this line from viewDidLoad method if you have-:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38
0

textLabel is a native field of UITableViewCell. It's defined as an Optional, and thats why it won't crash when the text you set is nil.

The custom label you are setting is probably defined as a-non optional label (probably an IBOutlet). Make sure you connect the label from your storyboard correctly by dragging the label to the correct outlet.

Lirik
  • 3,167
  • 1
  • 30
  • 31
  • In the code that works (with a table and a custom cell) the labels are defined as IBOutlets. The code that works and the code that doesn't is identical apart from the fact that the one that doesn't work contains 2 tables. The labels that I can't seem to use are also defined as IBOutlets – ZiEiTiA Feb 19 '17 at 14:24
  • Are you managing the 2 tables in the same code? 1. make sure both tables are connected. 2. check inside the tableView delegates (and datasource) methods you are working with the correct table that defined the custom cell accordingly. – Lirik Feb 19 '17 at 14:45
  • I know that it is referring to the correct table as the built in textLabel.text works ok. I have clicked on the cell in the table and set its class to the custom class that I am using for the cells - thats all I need to do? – ZiEiTiA Feb 19 '17 at 17:19
0

You need to register your custom cell as:

tableView.registerNib(UINib(nibName: "Your Nib Name", bundle: nil), forCellWithReuseIdentifier: "Your Cell Identifier")
Sumeet.Jain
  • 1,533
  • 9
  • 26