[Solved] How to load JSON array data into UITableView Section and Row using Swift?

[ad_1]

To display the JSON in sections efficiently you have to decode the JSON into a struct with a title member

struct Root : Decodable {
    let status : Bool
    let sections : [Section]

    private enum CodingKeys : String, CodingKey { case status, data }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        status = try container.decode(Bool.self, forKey: .status)
        let data = try container.decode([String:[Result]].self, forKey: .data)
        sections = data.compactMap{ return $0.value.isEmpty ? nil : Section(title: $0.key, result: $0.value) }
    }
}

struct Section {
    let title : String
    let result : [Result]
}

struct Result : Decodable {
    let id, name, date : String
    let group : [String]
}

Declare a data source array

var sections = [Section]()

Assign the result to the array

do {
    let decoder = try JSONDecoder().decode(Root.self,  from: data!)
    let status = decoder.status

    if status == true {
        sections = decoder.sections
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    } else {

    }
} catch { print(error) }

The relevant table view data source methods are

override func numberOfSections(in tableView: UITableView) -> Int {
    return sections.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections[section].result.count
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sections[section].title
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath)
    let item = sections[indexPath.section].result[indexPath.row]
    // Update the UI
}

Side note: Name your structs with more meaningful names. For example an array is supposed to be named with something in plural form (like in my previous suggestion)

18

[ad_2]

solved How to load JSON array data into UITableView Section and Row using Swift?