-4

How to set this json in a tableView.

{
   "page":0,
   "pageSize":5,
   "totalPageCount":16,
   "wkda":{
      "020":"Abarth",
      "040":"Alfa Romeo",
      "042":"Alpina",
      "043":"Alpine",
      "057":"Aston Martin"
   }
}
Witek Bobrowski
  • 3,749
  • 1
  • 20
  • 34
wdp
  • 23
  • 1
  • 4

1 Answers1

0

Create a struct which represents your data

struct YourObjectName: Codable {
    let page, pageSize, totalPageCount: Int
    let wkda: [String: String]
}

When getting your data from the server, parse your json into your newly created object, and store that object somewhere in your viewController.

let yourObject = try? newJSONDecoder().decode(YourObjectName.self, from: jsonData)

When configuring the tableView, set the number of rows based on your data:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourObject.count // Or something similar, based on your needs
}

Finally use your data to populate the cells:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: "SomeIdentifier", for: indexPath)
   // Populate your cell
   return cell
}

Also I encourage you to read a bit about TableView. Here is a good source to start: TableViews

πter
  • 1,899
  • 2
  • 9
  • 23