ios - 我需要将解析后的数据显示到 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 中显示它
解决方案
你需要
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]
推荐阅读
- java - 创建引用可以在带有 objectify 的事务内引发 ConcurrentModificationException
- javascript - 与父子元素相关的问题
- java - DynamoDB 嵌套映射到 Java 对象
- nuxt.js - Tailwind+ Nuxt:@apply 不适用于所有课程
- python - 是否可以将一个线程的结果通过管道传输到另一个线程?
- terraform - 错误:不允许对具有 VCS 连接的工作区应用
- python-3.x - 如何使用 BeautifulSoup 抓取标签之间的嵌套文本?
- ios - RAD Studio 10.4.1 尝试运行“无法创建进程:/Applications/PAServer-21.0.app/Contents/MacOS/dbkosx_27_0”时出现我的第一个 iOS 应用程序错误
- machine-learning - 就大 O 表示法而言,O(N*P) 是什么类别,P 表示特征大小,如在朴素贝叶斯或 kNN 中所见?
- reactjs - 使用特快专递和反应