JSON 使用 SwiftyJSON 和 Alamofire 的 TableView 数据
JSON data on TableView using SwiftyJSON and Alamofire
我想在 Table 视图中显示数据。问题是,我无法让它显示在上面。我的代码如下所示,希望有人能帮助我。
import Foundation
struct HeroStats {
let localized_name: String
let primary_attr: String
let attack_type: String
let legs: String
let img: String
}
这是我的 viewController 代码:
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var heros = [HeroStats]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
downloadJson()
self.tableView.reloadData()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return heros.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text =
heros[indexPath.row].localized_name.capitalized
return cell
}
//MARK: Parsing JSON data
func downloadJson(){
Alamofire.request("https://api.opendota.com/api/heroStats").responseJSON { response in
if let value = response.result.value {
let json = JSON(value)
//Printing strings from a JSON Dictionary
print(json[0]["localized_name"].stringValue)
print(json[0]["primary_attr"].stringValue)
print(json[0]["attack_type"].stringValue)
print(json[0]["legs"].stringValue)
print(json[0]["img"].stringValue)
}
} // End Alamofire call
}
}
请求是async
。当它给你一个结果时,你需要解析它并将它分配给你的 HeroStat
数组。这需要在您的请求完成处理程序中完成。收到回复后,您目前什么都不做。此外,您对 reloadData 的调用是在您实际收到响应之前完成的,因此不会这样做。
您可以做的是:收到响应后,填充 heros
数组并调用 tableView.reloadData()
以使其获取新填充的 HeroStat
值。
我想在 Table 视图中显示数据。问题是,我无法让它显示在上面。我的代码如下所示,希望有人能帮助我。
import Foundation
struct HeroStats {
let localized_name: String
let primary_attr: String
let attack_type: String
let legs: String
let img: String
}
这是我的 viewController 代码:
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var heros = [HeroStats]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
downloadJson()
self.tableView.reloadData()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return heros.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text =
heros[indexPath.row].localized_name.capitalized
return cell
}
//MARK: Parsing JSON data
func downloadJson(){
Alamofire.request("https://api.opendota.com/api/heroStats").responseJSON { response in
if let value = response.result.value {
let json = JSON(value)
//Printing strings from a JSON Dictionary
print(json[0]["localized_name"].stringValue)
print(json[0]["primary_attr"].stringValue)
print(json[0]["attack_type"].stringValue)
print(json[0]["legs"].stringValue)
print(json[0]["img"].stringValue)
}
} // End Alamofire call
}
}
请求是async
。当它给你一个结果时,你需要解析它并将它分配给你的 HeroStat
数组。这需要在您的请求完成处理程序中完成。收到回复后,您目前什么都不做。此外,您对 reloadData 的调用是在您实际收到响应之前完成的,因此不会这样做。
您可以做的是:收到响应后,填充 heros
数组并调用 tableView.reloadData()
以使其获取新填充的 HeroStat
值。