在Swift TableView中显示JSON数据的问题

时间:2016-06-12 11:18:56

标签: ios arrays json swift uitableview

我在TableView中显示JSON数组时遇到问题 - 结果是一个空白表,我知道它是正确设置的,因为它与固定数据数组一起使用。我似乎能够正确地解析JSON,因为我能够在控制台中操作打印输出,我认为问题是重载表功能但是我已经尝试了我在网上找到的解决方案的每个变体,似乎无法让它工作。 实际的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 个答案:

答案 0 :(得分:1)

您永远不会将服务器的结果分配给var locations = [LocationResponse]()变量,并且它始终为空。

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