0

I have the following code which parses JSON data from a website. I extracted the data in the form of a dictionary. Now I need to display each element of the dictionary as a cell in the Tableview. Below is my code:

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)
            }
        }
    }
}
}

So, now I have a dictionary in ordersSummary. How do i display it in the TableView

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Learner
  • 19
  • 7
  • Have you googled for a tutorial on UITableView? – Fabian Jul 21 '18 at 23:13
  • Yes. In my case, the JSON is a bit complicated and after some manipulation I got a dictionary in this format {"alaska":2, "alabama":9,"california":6}. I am clueless as to how to display it in a table view. In the tutorials, they mostly talk about similiar examples and its always array. If you know some particular tutiorial, you can share it's link here – Learner Jul 21 '18 at 23:17
  • 1
    Your first problem is that dictionaries are unordered, which is why you really want an array. You need to determine what information you want in the table and then iterate over the dictionary to build the relevant array – Paulw11 Jul 21 '18 at 23:23
  • UITableView is generally index-based, meaning you need to access the data in the dictionary based on an index. You can [make an array out of a dictionary](https://stackoverflow.com/questions/31845421/how-to-convert-dictionary-to-array), So what Paul said. And [custom cells](https://www.ralfebert.de/ios-examples/uikit/uitableviewcontroller/custom-cells/) might be helpful later. – Fabian Jul 21 '18 at 23:25

1 Answers1

-1

You need

var tableDic = [String: Int]()

//

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

//

in numberOfRows return

return self.tableDic.keys.count

in cellForRowAt use

let key = Array(self.tableDic.keys)[indexPath.row]
let currentInt = self.tableDic[key]
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • This will not give stable results. The dictionary isn't ordered. The resulting array of keys may come back in any order. Never use a dictionary as the basis of the data source of a table view. Put the data in an array. This is also inefficient having to recreate the same array over and over for each cell. Build a single array once when the data is obtained and then used that fixed array as the data model for the table. – rmaddy Jul 22 '18 at 00:58
  • @rmaddy i know it's unordered ( every array of keys will return in unordered but the same any place i put it ) , also the op doesn't state that he care about ordering – Shehata Gamal Jul 22 '18 at 08:03