2016-06-12 15 views
0

TableViewにJSON配列を表示する際に問題が発生しました。結果として、空のテーブルが固定データ配列で正しく動作するように設定されています。私はコンソールで印刷出力を操作できるようにJSONを正しく解析できるようですが、問題はリロードテーブル関数だと思いますが、私がオンラインで見つけたソリューションのあらゆるバリエーションを試してみたそれを働かせる。 実際のJSONは次のようになります。Swift TableViewでJSONデータを表示する際の問題

[{ 
"locationNumber": 10, 
"locationName": "Test Site", 
"locationPhoneNumber": "", 
"locationAddress": "test address" 
}, 
{ 
"locationNumber": 99, 
"locationName": "Test Site2", 
"locationPhoneNumber": "", 
"locationAddress": "second test address" 
}] 

LocationTableViewController

import UIKit 
import Alamofire 
import AlamofireObjectMapper 

class LocationTableViewController: UITableViewController { 

var locations = [LocationResponse]() 

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

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 
override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
      return 1} 

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cellIndentifier = "Cell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIndentifier, forIndexPath: indexPath) as! LocationTableViewCell 

    //configure the cell 
    cell.locationLabel.text = locations[indexPath.row].locationName 
    cell.locationidLabel.text = "$\(locations[indexPath.row].locationID)" 
    return cell 
} 

func getLocationResponse() { 
    let URL = "https://test.com.au/api/Store?uid=7654380A-D18F-46A1&username=app&password=apptest" 
    Alamofire.request(.GET, URL).responseArray { (response: Response<[LocationResponse], NSError>) in 
     let locationArray = response.result.value 
     if let locationArray = locationArray { 
      for location in locationArray { 
       print(location.locationID) 
       print(location.locationName) 
      } 
     } 
    } 
} 

LocationResponse.swift

import Foundation 
import ObjectMapper 

class LocationResponse: Mappable { 
var locationName: String? 
var locationID: Int? 

required init?(_ map: Map){ 
} 

func mapping(map: Map) { 
    locationName <- map["locationName"] 
    locationID <- map["locationNumber"] 
} 
} 

カスタムセル:

import UIKit 

class LocationTableViewCell: UITableViewCell { 

@IBOutlet var locationLabel: UILabel! 
@IBOutlet var locationidLabel: UILabel! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
} 

} 

私が正しくて、私は推測する配列が正しく作成されていることを意味し、印刷機能からコンソールでこれを取得しています:

Optional(10) 
Optional("Test Site") 
Optional(99) 
Optional("Test Site2") 

あなたは

答えて

1

そのサーバからの結果を割り当てることはできません場合は助けてください変数はvar locations = [LocationResponse]()で、常に空です。

if let locationArray = locationArray { 
    self.locations = locationArray 
    self.tableView.reloadData() 
} 
+0

固定!どうもありがとうございます! – Hound

+0

今すぐに受け入れました、ありがとう – Hound

+0

(あなたが十分な担当者と有益なユーザーの回答を得たら戻ってくる必要がありますが、感謝しています) –

関連する問題