我需要将解析后的数据显示到TableVIew

时间:2018-07-21 23:10:08

标签: ios json swift uitableview

我有以下代码,用于解析来自网站的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中显示

1 个答案:

答案 0 :(得分:-1)

您需要

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]