neX neX - 5 months ago 22
iOS Question

JSON data not showing up on UITableView

I'm using Alamofire for parsing JSON and I retrieve the correct JSON result on console, but it's not showing up on UITableView. I think it's race condition, but I don't see how to fix it.

WeatherViewController.swift

import Foundation
import UIKit

class WeatherViewController: UITableViewController{

var weatherStore: WeatherStore!
var weather: Weather!


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(weatherStore.allWeathers.count)
return weatherStore.allWeathers.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell", forIndexPath: indexPath)
let weather = weatherStore.allWeathers[indexPath.row]

cell.textLabel?.text = String(weather.id)
cell.detailTextLabel?.text = weather.main

return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

}
override func viewDidLoad() {
super.viewDidLoad()

weatherStore.createWeather { (fetchedWeather: Weather) in
self.weatherStore.allWeather.append(fetchedWeather)
}
tableView.reloadData()
let statusNarHeight = UIApplication.sharedApplication().statusBarFrame.height

let insets = UIEdgeInsets(top: statusNarHeight, left: 0, bottom: 0, right: 0)

tableView.contentInset = insets
tableView.scrollIndicatorInsets = insets
tableView.reloadData()

}
}


WeatherStore.swift

Edit: I commented out afterWeatherCreated(newWeather!) because after Harshai comment I see that I donĀ“t need it there.

import Foundation
import Alamofire


class WeatherStore {


var allWeather = [Weather]()


func createWeather(afterWeatherCreated: (Weather) -> Void) {
var id: Int = 9
var main: String = ""
var newWeather: Weather?
Alamofire.request(.GET, "http://api.openweathermap.org/data/2.5/weather?xyz")
.responseJSON(completionHandler: { response in

if let JSON = response.result.value {
// id = JSON["id"] as! Int
//main = JSON["main"] as! String
let dataArray: NSArray = JSON["weather"] as! NSArray

for item in dataArray {
let obj = item as! NSDictionary
for (key, value) in obj {
if key.isEqual("id"){
id = value as! Int
}
if key.isEqual("main"){
main = value as! String
}
}
}

}

newWeather = Weather(id: id, main: main)
//afterWeatherCreated(newWeather!)

})
}
}


AppDeleage.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

let weatherStore = WeatherStore()

let weatherController = window!.rootViewController as! WeatherViewController

weatherController.weatherStore = weatherStore

return true
}

Answer

Try this:

override func viewDidLoad() {
        super.viewDidLoad()

        weatherStore.createWeather { (fetchedWeather: Weather) in
            self.weatherStore.allWeather.append(fetchedWeather)'
            self.tableView.reloadData()
        }

        let statusNarHeight = UIApplication.sharedApplication().statusBarFrame.height

        let insets = UIEdgeInsets(top: statusNarHeight, left: 0, bottom: 0, right: 0)

        tableView.contentInset = insets
        tableView.scrollIndicatorInsets = insets
        tableView.reloadData()

    }