2016-07-02 56 views
0

我使用Alamofire來解析JSON,並在控制檯上檢索正確的JSON結果,但它沒有顯示在UITableView上。我認爲這是競賽狀況,但我看不出如何解決它。JSON數據沒有顯示在UITableView

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

編輯:我註釋掉afterWeatherCreated(newWeather!),因爲Harshai評論後,我看到,我不需要它。

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 
} 
+0

這是什麼方法呢? afterWeatherCreated(newWeather!) –

+0

@HarshalBhavsar我可能不需要那種方法。但我仍然沒有看到我的問題的解決方案。 – neX

+0

您的網絡請求是異步的,所以您需要在數據獲取後重新加載您的tableView。你可以在'afterWeatherCreated''的代碼中添加它。 – SArnab

回答

0

試試這個:

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() 

    } 
+0

謝謝,這工作!只是想知道,你應該調用reloadData()兩次嗎?如果是,那麼爲什麼。 – neX

+0

這不是關於調用它兩次,這是因爲它是一個異步調用,您必須在獲得數據時重新加載數據。 – Maysam