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

}

Accepted Reply

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.

Replies

Could you explain where is updateUIWithWeatherData located ?

Where is weatherDataModel defined ?


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.

Do you mean that the following shows "test" in cell ?


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
    }


If so, could you add a print:

Normally, cells should be dequeued ; so test with the following (and add the print statement to check city value):


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        cell.textLabel?.text = weatherDataModel.city
        print("weatherDataModel.city", weatherDataModel.city)
        //cell.textLabel?.text = "test"


        return cell
    }

updateUIWithWeatherData is located before the tableview function.


So I have this:


func updateWeatherData(json : JSON) {

if let tempResult = json["main"]["temp"].double {

weatherDataModel.temperature = Int(tempResult - 273.15)

weatherDataModel.city = json["name"].stringValue

weatherDataModel.condition = json["weather"][0]["id"].intValue

weatherDataModel.weatherIconName = weatherDataModel.updateWeatherIcon(condition: weatherDataModel.condition)

updateUIWithWeatherData() //Here I call working function

}

else {

cityLabel.text = "No data"

}

}


Then updateUIWithWeatherData()
And then tableView function.
The text "test" does show.
Your code shows nothing.
So seems like the property "city" isn't set here.

When you say: Your code shows nothing.

What do you get as result of print ?


        print("weatherDataModel.city", weatherDataModel.city)


Could you show the complete code of the viewController ? To see where each function is called.

Usually we need also to use the indexPath in the function tableView (tableView: , cellForRowAt indexPath:). For example:


cell.textLabel?.text = weatherDataModel.city[indexPath.row]


But in your case probably you are not getting values from JSON file.

I am not at my computer right now, and will send the complete code when I am.


when I print out your code I get the string ”weatherDataModel.city” but not the value.


In ”updateWeatherData” I do get the values from the JSON-file but not in the TableView as it seems.


I don’t understand why.

I print out your code I get the string ”weatherDataModel.city” but not the value.


I guess it's for Claude31's reply and not for John368's.

Anyway readers don't understand what's happening without your code. Waiting for enough code.

Just means that the value is empty

print("weatherDataModel.city", weatherDataModel.city)


So, you get only "weatherDataModel.city"


So, we need to inspect your code to understand why, at this stage, weatherDataModel.city is empty string. Do you confirm that you get the cityname elsewhere ?

"Do you confirm that you get the cityname elsewhere ?"


Yes, I get weatherDataModel.city in updateUIWithWeatherData()

Well, show the complete code as soon as you get access to your computer.

I have tried to show the viewController but it said something about being moderated.
Is it visible to others?

No, not visible.


have you just copied the text ?


It should not contain URL or at least they must be edited as

h ttps://

Yes, just copied the text.


Have removed the links now (for the API)
Let me know if it is visible or what I else can do.

Trying to post bit by bit instead.

import UIKit


class WeatherDataModel {


//Declare your model variables here

var temperature : Int = 0

var condition : Int = 0

var city : String = ""

var weatherIconName : String = ""

//This method turns a condition code into the name of the weather condition image

func updateWeatherIcon(condition: Int) -> String {

switch (condition) {

case 0...300 :

return "tstorm1"

case 301...500 :

return "light_rain"

case 501...600 :

return "shower3"

case 601...700 :

return "snow4"

case 701...771 :

return "fog"

case 772...799 :

return "tstorm3"

case 800 :

return "sunny"

case 801...804 :

return "cloudy2"

case 900...903, 905...1000 :

return "tstorm3"

case 903 :

return "snow5"

case 904 :

return "sunny"

default :

return "dunno"

}


}

}

import UIKit


class TrainDataModel {

//Declare your model variables here

var station : String = ""

//var arrivalTime : Date? = nil

var arrivalTime : String = ""

}

import UIKit

import CoreLocation

import Alamofire

import SwiftyJSON



class WeatherViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource {

//Constants

let APP_ID_TRAIN2 =

let APP_ID_SEARCH_STATION =

let WEATHER_URL = "

let TRAIN_URL = "

//let TRAIN_URL =

let TRAIN_URL2 =

let TRAIN_URL3 =

let APP_ID =



//TODO: Declare instance variables here

let locationManager = CLLocationManager()

let weatherDataModel = WeatherDataModel()

let trainDataModel = TrainDataModel() //Object

//Pre-linked IBOutlets

@IBOutlet weak var weatherIcon: UIImageView!

@IBOutlet weak var cityLabel: UILabel!

@IBOutlet weak var temperatureLabel: UILabel!


override func viewDidLoad() {

super.viewDidLoad()

//TODO:Set up the location manager here.

locationManager.delegate = self

locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters

locationManager.requestWhenInUseAuthorization()

locationManager.startUpdatingLocation()

}