Ios 如何使用Swift在UItableView中显示订单部分列表

Ios 如何使用Swift在UItableView中显示订单部分列表,ios,swift,Ios,Swift,我在Codable的帮助下从JSON获取数据,并加载到TableView。这里,JSONmultiple array我正在接收,每个数组都有不同的键名。我在部分的tableview中显示的键名。现在,问题是JSON数组显示正确的顺序列表,但由于字典存储,我在tableview结果中得到了无序的列表。我需要显示JSON显示的相同的顺序 可编码的 struct Root : Decodable { let status : Bool let sections : [Section]

我在
Codable
的帮助下从
JSON
获取数据,并加载到
TableView
。这里,JSON
multiple array
我正在接收,每个数组都有不同的键名。我在
部分的tableview中显示的键名
。现在,问题是JSON数组显示正确的顺序列表,但由于
字典
存储,我在tableview结果中得到了
无序的
列表。我需要显示JSON显示的相同的
顺序

可编码的

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.map{ Section(title: $0.key, result: $0.value) }
        sections = data.compactMap{ return $0.value.isEmpty ? nil : Section(title: $0.key, result: $0.value) }
    }
}

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

struct Result : Decodable {
    let id, name, date : String
    let group : [String]
}
表格视图

// MARK: UITableview Delegates

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let currentSection = isFiltering ? filteredSections[section] : sections[section]
    return currentSection.result.count
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath) as! MyCustomCell
    let section = isFiltering ? filteredSections[indexPath.section] : sections[indexPath.section]
    let item = section.result[indexPath.row]
    cell.nameLabel.text = item.name
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //print("You tapped cell number \(indexPath.row).")
    let section = isFiltering ? filteredSections[indexPath.section] : sections[indexPath.section]
    let item = section.result[indexPath.row]
    print("\(item)")
}

不能对键进行排序,但可以定义键顺序

let keyOrder = ["Overdue", "Today", "Tomorrow", "Nextweek", "Future"]

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 = keyOrder.compactMap({ key -> Section? in
            guard let section = data[key], !section.isEmpty else { return nil }
            return Section(title: key, result: section)
        })
    }
}

您的表视图数据源和委托方法看起来非常熟悉—您的结构不能准确地表示json。似乎这不是一本任意的字典,你有一系列“过期”的条目,“今天”项目等。您应该将这些特定数组解码为特定属性,然后创建一个返回部分的计算变量。请参见三种解决方案:1)编辑API并获取对象数组2)向响应中添加另一个对象,该对象说明键的顺序,以便您可以在TableView中对它们进行排序3)在您的应用程序,以便对字典键进行排序。谢谢。上面的解决方案看起来像是硬编码的数组值。若我们在服务器端更改任何名称,那个么可能会发生崩溃,或者我需要再次更改为硬编码数组。那么,有什么替代方案吗?排序顺序完全是任意的。如果您负责后端,请发送一些可以排序的键,例如索引。谢谢。我在服务器端更改了索引并完成了正确的排序