iosswiftuitableview

JSON data on UITableView


I managed to include this API from America FAA web site in order to download the NOTAM for a pilot. I manage to send the request of the param "api key, location, state". I get back my JSON data and it works fine. Now my problem is I want to display on a tableView one item of the array that I got back in JSON format, is the item called 'all'.

I created the IBOutlet. I gave the cell identifier, but I'm stuck here:

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    //Constants
    let notamUrl = "https://v4p4sz5ijk.execute-api.us-east-1.amazonaws.com/anbdata/states/notams/notams-realtime-list"
    let api_key = "mykey"
    let notamModel = ModelloNotam()

    @IBOutlet weak var tableView: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return //????????? i dont know
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath)

        // ????????? i dont know
        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate=self
        tableView.dataSource=self

        func getNOTAM (url:String,parameters:[String:String]){
            Alamofire.request(url, method: .get, parameters: parameters).responseJSON {
                response in
                if response.result.isSuccess{
                    let notamJSON : JSON = JSON (response.result.value!)

                    self.displayNotam(json: notamJSON)
                } else{
                    print("errore connessione\(response.result.error)")
                }
            }
        }

        var locations = "VMMC"
        var state = "CHN"
        let params : [String : String] = ["locations" : locations, "state" : state, "api_key" : api_key]
        getNOTAM(url: notamUrl, parameters: params)
    }

    func displayNotam (json:JSON) {
        let conta = json.count

        for var i in 0...conta {
            i = i + 1
            notamModel.all = json [i]["all"].stringValue
            notamModel.type = json [i]["type"].stringValue
            //            print("The NOTAM type is \(notamModel.type)")
            //            print(notamModel.all)
            //            print("************************")
        }
    }
}

Solution

  • please refer this tutorial https://www.youtube.com/watch?v=sd7d4eoM54U&t=1857s and model data according to JSON file. networking request goes out of main execution queue.