0
votes

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! :)

2

2 Answers

2
votes

The problem is related to your data structure and the way you filter it out in cellForRowAtIndexPath.

First you filter out a single country by using let data = datas[indexPath.row], which gives you a dataset like the following:

{
    "name" : "Åland Islands",
    "flag" : "https:\/\/1fx.cash\/\/flags\/europe.png",
    "currency_code" : "EUR",
    "country_iso3" : "ALA",
    "country_code" : "AX"
}

Afterwards you try to access the countries property and the name property within that. The only problem is, there doesn't exist a countries property as you can see in the above snippet.

Instead you need to access the name property directly like the following code does:

if let caption = data["name"].string{
    cell.textLabel?.text = caption
}
0
votes

I think you need add this:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

in

override func viewDidLoad() {
          ...
}