首页 > 解决方案 > 我需要将解析后的数据显示到 TableVIew

问题描述

我有以下代码解析来自网站的 JSON 数据。我以字典的形式提取数据。现在我需要将字典的每个元素显示为 Tableview 中的一个单元格。下面是我的代码:

class OrdersSummaryFetchservice{
let shopifyOrdersListURL: URL?
var provinceNamesDictionary = [String: Int]()

init(URL: URL) {
    shopifyOrdersListURL = URL
}


func fetchOrdersSummary(completion: @escaping (OrdersSummary) -> Void) {
    Alamofire.request(shopifyOrdersListURL!).responseJSON { (response) in
        if let orderListInJSON = response.result.value as? [String:Array<Any>]{
            if let orderListArray = orderListInJSON["orders"] {
                for index in 0...(orderListArray as Array).count-1 {
                    if let order = orderListArray[index] as? [String:Any] {
                        if let billingAddress = order["billing_address"] as? [String:Any] {
                            if let provinceName = billingAddress["province"] as? String {
                                if (self.provinceNamesDictionary[provinceName] == nil) {
                                    self.provinceNamesDictionary[provinceName] = 1
                                } else {
                                    let orders = self.provinceNamesDictionary[provinceName]!
                                    self.provinceNamesDictionary[provinceName] = orders + 1
                                }
                            }
                        }
                    }
                }
                let ordersSummary = OrdersSummary(provinceNameDictionary: self.provinceNamesDictionary)
                completion(ordersSummary)
            }
        }
    }
}
}

所以,现在我在 ordersSummary 中有一本字典。如何在 TableView 中显示它

标签: iosjsonswiftuitableview

解决方案


你需要

var tableDic = [String: Int]()

//

let item = OrdersSummaryFetchservice(url:////)
item.fetchOrdersSummary { (result) in
   self.tableDic = result
   self.tableView.reloadData()
}

//

作为numberOfRows回报

return self.tableDic.keys.count

cellForRowAt使用中

let key = Array(self.tableDic.keys)[indexPath.row]
let currentInt = self.tableDic[key]

推荐阅读