So I'm connected to this API and I retrieve some data from it using ALAMOFIRE and SWIFTYJSON then display it to UItable view:
Here is the sample JSON Return:
{
"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"
}
]
}
And here is my entire viewcontroller:
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)
}
}
When I run it, it doesn't display anything.
I used println(row) so when I select a cell, it prints a data on the console to show that there really are data there, and there is.
I'm just having trouble displaying them on the UItable view.
Can anyone help me? Thanks in advance! :)