Ios 将JSON数据解析为UITableView节最终会复制并显示错误的节

Ios 将JSON数据解析为UITableView节最终会复制并显示错误的节,ios,swift,uitableview,Ios,Swift,Uitableview,我让我的应用程序获取JSON数据并显示数据,但当我尝试在部分中执行此操作时,我会在每个部分中获得重复的数据,并且部分太多。目标是使每个州成为一个部分,下面列出该州的每个奖金。这是我目前拥有的代码: 视图控制器: import UIKit import os.log class BonusListViewController: UITableViewController { var bonuses = [JsonFile.JsonBonuses]() override fun

我让我的应用程序获取JSON数据并显示数据,但当我尝试在部分中执行此操作时,我会在每个部分中获得重复的数据,并且部分太多。目标是使每个州成为一个部分,下面列出该州的每个奖金。这是我目前拥有的代码:

视图控制器:

import UIKit
import os.log

class BonusListViewController: UITableViewController {

    var bonuses = [JsonFile.JsonBonuses]()

    override func viewDidLoad() {
        super.viewDidLoad()

        //MARK: Trigger JSON Download
        downloadJSON {
        }
    }

    // MARK: - Table View Configuration
    // MARK: Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        print("Found \(bonuses.count) sections.")
        return bonuses.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("Found \(bonuses[section].bonusCode.count) rows in section.")
        return bonuses[section].bonusCode.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        cell.textLabel?.text = bonuses[indexPath.section].name.capitalized
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "showDetail", sender: self)
    }
    // MARK: - Table View Header
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return bonuses[section].state
    }
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 3
    }

    // MARK: Functions
    // MARK: - Download JSON from ToH webserver
    func downloadJSON(completed: @escaping () -> ()) {
        let url = URL(string: "http://tourofhonor.com/BonusData.json")
        URLSession.shared.dataTask(with: url!) { [weak self] (data, response, error) in
            if error == nil {
                do {
                    let posts = try JSONDecoder().decode(JsonFile.self, from: data!)
                    DispatchQueue.main.async {
                        completed()
                    }
                    print("JSON Version \(posts.meta.version) loaded.")
                    print(posts.bonuses.map {$0.bonusCode})
                    self?.bonuses = posts.bonuses
                    DispatchQueue.main.async {
                        //reload table in the main queue
                        self?.tableView.reloadData()
                    }
                } catch {
                    print("JSON Download Failed")
                }
            }
        }.resume()
    }

    // MARK: - Navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? BonusDetailViewController {
            destination.bonus = bonuses[(tableView.indexPathForSelectedRow?.row)!]
        }
    }
}
JsonFile.swift只是一个结构:

import Foundation

struct JsonFile: Codable {
    struct Meta: Codable {
        let fileName: String
        let version: String
    }
    struct JsonBonuses: Codable {
        let bonusCode: String
        let category: String
        let name: String
        let value: Int
        let city: String
        let state: String
        let flavor: String
        let imageName: String
    }
    let meta: Meta
    let bonuses: [JsonBonuses]
}

JSON文件包含两个状态和9个条目。我的print语句显示它找到了9个部分,每个部分中有3行。当我尝试将
返回奖金.count
设置为类似于
返回奖金.state.count
的值时,我得到一个错误,该错误表示“类型的值'[JsonFile.JsonBonuses]'没有成员“state.”,但在我的结构中显然有一个“state”项。

您需要一个数组数组来包含分段数据。您只有一个数组。您需要正确地组织数据。请注意,
奖金[section].bonusCode
为您提供了一个
字符串
bonus[section].bonusCode.count
提供该字符串中的字符数。我还可以完全控制JSON数据,因此,组织数据的最佳方式是什么,以便正确使用节(如果有帮助的话,我的下一个任务是添加搜索和筛选,以防数据的组织方式也应该有所不同)。你为什么需要分区?每个分区代表什么?首先回答这个问题。然后你可以将你的
奖金
数组组织为分区。每个奖金都位于特定的状态,因此分区将按状态组织所有奖金,以便于用户使用。我可以通过使用数组(set()来做到这一点吗不知何故,也许是使用map函数?我正确地解析了JSON,因此“查看JSON数据,按状态分组,然后显示每个唯一的状态”似乎不是一个非常复杂的函数。