JSON 显示到表视图
JSON displayed to tableview
我想第一次使用 AlamofireObjectMapper 来解析 swift 中的 json 响应。
我是这样映射的:
class ModelCurrency: Mappable {
var success : Bool?
var terms : String?
var privacy : String?
var timestamp : CGFloat?
var source : String?
var quotes : [Quotes]?
init() {}
required init?(map: Map) {
}
func mapping(map: Map) {
success<-map["success"]
terms<-map["terms"]
privacy<-map["privacy"]
timestamp<-map["timestamp"]
source<-map["source"]
quotes<-map["quotes"]
print("It json\(terms)")
}
}
class Quotes : Mappable {
var name : String?
var val : CGFloat?
required init?(map: Map) {
}
func mapping(map: Map) {
name<-map["name"]
val<-map["val"]
}
}
和我的控制器
var arrayTable = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
super.viewDidLoad()
let URL = "http://www.apilayer.net/api/live?access_key=ad847a0a855c0647590df2b818923025"
Alamofire.request(URL).responseObject { (response: DataResponse<ModelCurrency>) in
let currency = response.result.value!;
for quotes in currency.quotes! {
self.arrayTable.append(quotes.name!)
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 5}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayTable.count }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as UITableViewCell
cell.textLabel?.text = self.arrayTable[indexPath.row]
return cell
}
我想要在表格视图中显示数组引号。怎么做 ?
我认为您需要reload table 更新数据源后查看 arrayTable
:
...
for quotes in currency.quotes! {
self.arrayTable.append(quotes.name!)
}
self.tableView.reloadData()
...
此外,您不需要调用 super.viewDidLoad()
两次。
我想第一次使用 AlamofireObjectMapper 来解析 swift 中的 json 响应。 我是这样映射的:
class ModelCurrency: Mappable {
var success : Bool?
var terms : String?
var privacy : String?
var timestamp : CGFloat?
var source : String?
var quotes : [Quotes]?
init() {}
required init?(map: Map) {
}
func mapping(map: Map) {
success<-map["success"]
terms<-map["terms"]
privacy<-map["privacy"]
timestamp<-map["timestamp"]
source<-map["source"]
quotes<-map["quotes"]
print("It json\(terms)")
}
}
class Quotes : Mappable {
var name : String?
var val : CGFloat?
required init?(map: Map) {
}
func mapping(map: Map) {
name<-map["name"]
val<-map["val"]
}
}
和我的控制器
var arrayTable = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
super.viewDidLoad()
let URL = "http://www.apilayer.net/api/live?access_key=ad847a0a855c0647590df2b818923025"
Alamofire.request(URL).responseObject { (response: DataResponse<ModelCurrency>) in
let currency = response.result.value!;
for quotes in currency.quotes! {
self.arrayTable.append(quotes.name!)
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 5}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayTable.count }
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as UITableViewCell
cell.textLabel?.text = self.arrayTable[indexPath.row]
return cell
}
我想要在表格视图中显示数组引号。怎么做 ?
我认为您需要reload table 更新数据源后查看 arrayTable
:
...
for quotes in currency.quotes! {
self.arrayTable.append(quotes.name!)
}
self.tableView.reloadData()
...
此外,您不需要调用 super.viewDidLoad()
两次。