Showing property in tablecell

I am following a course and am trying so test myself.
I have a model and I give values to properties from a json-file.
(If Im showing code wrong please tell me how I shoud do it)


This works:

func updateUIWithWeatherData() {

cityLabel.text = weatherDataModel.city

temperatureLabel.text = String(weatherDataModel.temperature)

weatherIcon.image = UIImage(named: weatherDataModel.weatherIconName)

}


But here it doesn't. Test shows but not the city.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")

cell.textLabel?.text = weatherDataModel.city

//cell.textLabel?.text = "test"

return cell

}

When you get Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value, the first thing you need to do is finding on which line this runtime-error has occurred.


Something is nil, that should not be nil included in the line.


For example, if you get that error on this line:

tableView.delegate = self

Then `tableView` is nil, that should not be nil. This happens when you do not connect the IBOutlet properly.


So, on which line that is happening?

Yes, it is at the line

tableView.delegate = self
That the error fist occurs.

But I can show a value set in
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


But not the value from the model.
So I can do


cell.textLabel?.text = "test"


But not


cell.textLabel?.text = self.weatherDataModel.city


But I know that the value is set in function


updateWeatherData



Key question: have you checked that tableView IBOutlet is effectively connected to the UITableView in IB ?

Not sure.
I connected the outlet to source and delegate.
If this was working I'm not sure.
How can I check this?

Accepted Answer

But I can show a value set in


That has nothing to do with IBOutlet connection.


You need to open the storyboard editor and connect your Table View in design canvas to the IBOutlet `tableView` in the code.

When you look at the code, you have a white circle on the left of

@IBOutlet weak var tableView: UITableView!


If the circle is empty, you are not connected.

If black you are connected to something. If you hover the mouse over the dot, whist having the storyboards opened in other pane, you should see the tableView being hilited.


If not:

- drag from the circle to the tableView in storyboard

- release when tableView gets hilited.

It worked!


Thank you both!
I'm sorry that I couldn't mark both as correct answers.
Clearly I have to learn more about the basics.
One question: Could I have made this connection completely in code?


Question two: I want to display two values in one cell. The staition from the API on the left and the time on the right.


Without explaining everything, could you give me a hint to the right solution?

Showing property in tablecell
 
 
Q