Итак, я подключен к этому API и получаю от него некоторые данные с помощью ALAMOFIRE и SWIFTYJSON, а затем отображаю их в представлении UItable:
Вот пример возврата JSON:
{
"status" : "success",
"countries" : [
{
"name" : "Åland Islands",
"flag" : "https:\/\/1fx.cash\/\/flags\/europe.png",
"currency_code" : "EUR",
"country_iso3" : "ALA",
"country_code" : "AX"
},
{
"name" : "American Samoa",
"flag" : "https:\/\/1fx.cash\/\/flags\/usa.png",
"currency_code" : "USD",
"country_iso3" : "ASM",
"country_code" : "AS"
},
{
"name" : "Virgin Islands, U.S.",
"flag" : "https:\/\/1fx.cash\/\/flags\/usa.png",
"currency_code" : "USD",
"country_iso3" : "VIR",
"country_code" : "VI"
}
]
}
А вот и весь мой вьюконтроллер:
import UIKit
import Alamofire
import SwiftyJSON
class addCountry: UIViewController, UITableViewDataSource, UITableViewDelegate {
var datas: [JSON] = []
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(.GET, "https://sample.com/countries").responseJSON { (request, response, json, error) in
if json != nil {
var jsonObj = JSON(json!)
if let data = jsonObj["countries"].arrayValue as [JSON]?{
self.datas = data
self.tableView.reloadData()
}
}
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.datas.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
let data = datas[indexPath.row]
if let caption = data["countries"]["name"].string{
cell.textLabel?.text = caption
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = datas[indexPath.row]
println(row)
}
}
Когда я запускаю его, он ничего не отображает.
Я использовал println(row), поэтому, когда я выбираю ячейку, она печатает данные на консоли, чтобы показать, что там действительно есть данные, и они есть.
У меня просто проблемы с отображением их в представлении UItable.
Может кто-нибудь мне помочь? Заранее спасибо! :)