【问题标题】:Loading JSON data to the tableview not working将 JSON 数据加载到 tableview 不起作用
【发布时间】:2017-06-12 19:31:46
【问题描述】:

下面是我的代码。 这里 print(currency) 函数正在工作..这意味着检索到 json 数据。但不显示在 tableview 中。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
@IBOutlet weak var tableView: UITableView! 
var TableData:Array< String > = Array < String >()

override func viewDidLoad()
{
   super.viewDidLoad()  
   get_data_from_url("http://api.fixer.io/latest")
}

表格视图部分

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

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
    cell.textLabel?.text = TableData[indexPath.row] 
    return (cell)
}

JSON 数据检索

func get_data_from_url(_ link:String)
{
    let url = URL(string: "http://api.fixer.io/latest")

    let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
        if error != nil
        {
            print ("ERROR")
        }
        else
        {
            if let content = data
            {
                do
                {
                    //Array
                    let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject

                    if let rates = myJson["rates"] as? NSDictionary
                    {
                        if let currency = rates["NOK"] as? String
                        {
                            print(currency)
                            self.TableData.append(currency)
                            self.tableView.reloadData()
                        }
                    }
                }
                catch
                {

                }
            }
        }
    }
    task.resume()


}

数据显示在带有打印功能的 xcode 中。所以很明显,数据正在检索。但问题是在将数据加载到 tableview 时。
请帮我加载它。

【问题讨论】:

  • 从 tableView 方法中删除 public 还要检查您的 tableView delegatedataSourece 是否与您的 viewController 正确连接。
  • 再次它不起作用@NiravD
  • DispatchQueue.main.async { self.tableView.reloadData() }一样在主线程上重新加载tableView
  • 对不起。挪威克朗是双倍的。下面的答案有帮助。感谢您的帮助@NiravD

标签: ios json uitableview swift3


【解决方案1】:

获取数据..(您的 NOK 不是 String.. 它的 Double)

let myJson = try JSONSerialization.jsonObject(with: content, options:[]) as [String:Any]

                if let rates = myJson["rates"] as? [String:Double],  
                   let currency = rates["NOK"] 
                {
                    print(currency)
                    self.tableData.append(String(currency))
                    DispatchQueue.main.async { 
                        self.tableView.reloadData() 
                    }
                }

// Declate array of String  
var tableData = [String]()

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    cell.textLabel?.text = tableData[indexPath.row] 
    return cell
}

【讨论】:

  • 谢谢@El Captain v2.0。
  • @El Captain v2.0 ,要加载所有费率,代码中需要进行哪些更改。我对字符串感到困惑,双倍..请帮助我
  • 你的表格单元格是什么样子的?
  • 上面我只添加了挪威克朗的汇率。我需要来自该链接的完整数据
猜你喜欢
  • 1970-01-01
  • 2018-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多