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

时间:2018-06-10 23:14:39

标签: ios swift uitableview

我有我的应用程序获取JSON数据并显示数据,但是当我尝试分段时,我会得到每个部分的重复项和太多部分。目标是每个州都是一个部分,下面列出了该州的每个奖金。这是我目前的代码:

的ViewController:

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个条目。我的打印报表显示它找到了9个部分,每个部分有3行。当我尝试将return bonuses.count设置为return bonuses.state.count之类的内容时,我收到一条错误,上面写着“类型的值'[JsonFile.JsonBonuses]”没有成员'state'。“但我的结构中显然有一个“状态”项目。

0 个答案:

没有答案