我正在冒险进入编程世界,并且遇到了JSON让人头疼的问题。我使用的是Swift 3,SwiftyJSON和Alamofire
这是我正在查看的JSON数据:
{
"status": "OK",
"latlng": [55.7182304595, 12.4136193718],
"northingseastings": [6180000, 714400],
"results": [{
"frequencyAreas": [
[470, 485],
[495, 517],
[527, 541],
[559, 565],
[575, 581],
[591, 613],
[623, 645],
[655, 709],
[719, 725],
[743, 773],
[783, 790]
],
"guardBand": 1,
"lastChannel": 60,
"tvChannelsNoGuardBand": [
[21, 22],
[24, 26],
[28, 29],
[32, 32],
[34, 34],
[36, 38],
[40, 42],
[44, 50],
[52, 52],
[55, 58],
[60, 60]
]
}]
}
我需要做的是将"frequencyAreas"
中的所有内容添加到UITableView
。
我可以使用viewDidLoad
内的以下内容轻松获得所有频率:
let url = "https://frekvens.erhvervsstyrelsen.dk/findKanalerAPI.php?output=JSON&language=da&northings=6180000&eastings=714400"
Alamofire.request(url, method: .get).validate().responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
if let _ = json["results"].array {
for result in json["results"].arrayValue {
let frequencies: Array<JSON> = result["frequencyAreas"].arrayValue
print(frequencies)
}
} else {
print("json error: \(String(describing: json.error))")
}
case .failure(let error):
print(error)
}
}
但我完全不知道如何将frequencyAreas
中的数据添加到UITableView
。
这是我拨打print(frequencies)
时得到的结果:
[[
470,
485
], [
495,
517
], [
527,
541
], [
559,
565
], [
575,
581
], [
591,
613
], [
623,
645
], [
655,
709
], [
719,
725
], [
743,
773
], [
783,
790
]]
我希望看到的是,嵌套数组被添加到UITableView中,如下所示:
格1:470,485
格2:495,517
格子3:527,541
格4:559,565
等
这是我到目前为止所拥有的。这只返回第一个元素(即470)并将其添加到单元格中。
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return frequencyList.count
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? frequencyTableViewCell
for (index, value) in frequencyList.enumerated() {
frequencyList[index] = value
let bands = frequencyList[index]
print(frequencyList)
cell?.title.text = String(describing: bands[index][indexPath.row])
}
return cell!
}
感谢。