首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UITableView上的JSON数据

UITableView上的JSON数据
EN

Stack Overflow用户
提问于 2018-06-20 00:00:30
回答 2查看 80关注 0票数 0

我设法从美国联邦航空局的网站上包含了这个API,以便下载NOTAM作为一个试点。我设法发送了参数"api key,location,state“的请求。我得到了我的JSON数据,它工作得很好。现在我的问题是,我想要在tableView上显示我以JSON格式返回的数组中的一项,即名为'all‘的项。

我创建了IBOutlet。我给了手机识别码,但我被困在这里了

代码语言:javascript
复制
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("************************")
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-20 03:31:45

请根据JSON文件参考本教程的https://www.youtube.com/watch?v=sd7d4eoM54U&t=1857s和模型数据。网络请求离开主执行队列。

票数 0
EN

Stack Overflow用户

发布于 2018-06-23 10:02:55

我用这个代码来解决。!!

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

//让text = arrayindexPath.row // cell.textLabel?.text=text

代码语言:javascript
复制
    (cell.contentView.viewWithTag(1) as! UILabel).text = array[indexPath.row]
    (cell.contentView.viewWithTag(2) as! UILabel).text = array2[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count

}

谢谢你的帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50932555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档