【问题标题】:Issue displaying JSON data in Swift TableView在 Swift TableView 中显示 JSON 数据的问题
【发布时间】:2016-06-12 11:18:56
【问题描述】:

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

}

我在控制台中从 print 函数中得到这个,看起来正确,我猜这意味着数组已正确创建:

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

如果可以的话请帮忙

【问题讨论】:

    标签: ios arrays json swift uitableview


    【解决方案1】:

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

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

    【讨论】:

    • (一旦你有足够的代表和投票用户的答案,你应该回来。你不必这样做,但它表示感谢。)
    猜你喜欢
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    相关资源
    最近更新 更多