2016-07-02 13 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:Harshaiのコメントの後、私はそこにそれを必要とドントことを見るので、私はafterWeatherCreated(!newWeather)からコメントを。

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()を2回呼び出すべきでしょうか?はいの場合はなぜですか。 – neX

+0

2回呼び出すことではなく、非同期呼び出しでデータを取得したときにデータをリロードする必要があるからです。 – Maysam

関連する問題