简体   繁体   English

在Swift TableView中显示JSON数据时出现问题

[英]Issue displaying JSON data in Swift TableView

I'm having an issue displaying a JSON array in a TableView - result is a blank table which i know is set up correctly as it works with a fixed data array. 我在TableView中显示JSON数组时遇到问题-结果是一个空白表,我知道它可以正确设置,因为它可以与固定数据数组一起使用。 I seem to be able to parse the JSON correctly as i'm able to manipulate the print output in the console, I think the problem is a reload table function however I have attempted every variation of a solution I have found online and cannot seem to get it to work. 我似乎能够正确解析JSON,因为我能够在控制台中操作打印输出,我认为问题是重载表功能,但是我尝试了在线找到的各种解决方案,但似乎无法让它工作。 The actual JSON looks like this: 实际的JSON如下所示:

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

The LocationTableViewController 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)
            }
        }
    }
}

The LocationResponse.swift 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"]
}
}

Custom Cell: 自定义单元:

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

}

I'm getting this in the console from the print function which looks correct and I guess means the array has been created correctly: 我在控制台中从看起来正确的打印功能中得到了这个,我想这意味着数组已正确创建:

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

Please help if you can 如果可以的话请帮忙

You never assign the result from server to the var locations = [LocationResponse]() variable and it's always empty. 您永远不会将服务器的结果分配给var locations = [LocationResponse]()变量,并且该变量始终为空。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM