首页 > 解决方案 > Swift - 如何使用字典为 tableView 单元格提供文本?

问题描述

我有以下 JSON 文件:

[
{
"countryName": "Afghanistan",
"continent": "Asia",
"population": 34656032,
"currency": "Afghani"
},
{
"countryName": "Albania",
"continent": "Europe",
"population": 2876591,
"currency": "Lek"
},
{
"countryName": "Bulgaria",
"continent": "Europe",
"population": 7050034,
"currency": "Lev"
},
{
"countryName": "Finland",
"continent": "Europe",
"population": 5517887,
"currency": "Euro"
},
{
"countryName": "Iceland",
"continent": "Europe",
"population": 350710,
"currency": "Icelandic Krona"
},
{
"countryName": "Japan",
"continent": "Asia",
"population": 126672000,
"currency": "Yen"
},
{
"countryName": "Oman",
"continent": "Middle East",
"population": 4424762,
"currency": "Rial"
},
{
"countryName": "South Africa",
"continent": "Africa",
"population": 57725600,
"currency": "South African Rand"
},
{
"countryName": "Uruguay",
"continent": "South America",
"population": 3444006,
"currency": "Uruguayan Peso"
},
{
"countryName": "Venezuela",
"continent": "South America",
"population": 31568179,
"currency": "Bolivar Soberano"
}
]

使用 SwiftyJson 将其解析到我的项目中后,我希望我的 tableView 中的每个单元格都有“countryName”的标题,所以有以下代码:

import UIKit

class ViewController: UITableViewController {

var countries = [[String: String]]()
var numberOfCountries = 0

override func viewDidLoad() {
    super.viewDidLoad()

    title = "Select country"

    if let jsonPath = Bundle.main.path(forResource: "Countries", ofType: "json") {

        if let data = try? String(contentsOfFile: jsonPath) {

            print("data: \(data)")

            let json = JSON(parseJSON: data)

            parse(json: json)

            print("countries: \(countries)")

        } else {
            print("Unable to get contents of JSON file")
        }
    }

}

func parse(json: JSON) {
    for result in json.arrayValue {
        let countryName = result["countryName"].stringValue
        let continent = result["continent"].stringValue
        let population = result["population"].stringValue
        let currency = result["currency"].stringValue

        let obj = ["countryName": countryName, "continent": continent, "population": population, "currency": currency]

        countries.append(obj)
    }
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let numberOfCountries = countries.count
    return numberOfCountries
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Country", for: indexPath)
    var cellTitle = ""

    for item in countries {
        cellTitle = item["countryName"] as! String
        print("cellTitle: \(cellTitle)")
    }

    cell.textLabel?.text = cellTitle
    return cell
}

但是,该数组只是循环了十次,委内瑞拉被选为每个 tableView 单元格的标题。

我的调试控制台出现如下:

countries: [["countryName": "Afghanistan", "continent": "Asia", "population": "34656032", "currency": "Afghani"], ["countryName": "Albania", "continent": "Europe", "population": "2876591", "currency": "Lek"], ["countryName": "Bulgaria", "continent": "Europe", "population": "7050034", "currency": "Lev"], ["countryName": "Finland", "continent": "Europe", "population": "5517887", "currency": "Euro"], ["countryName": "Iceland", "continent": "Europe", "population": "350710", "currency": "Icelandic Krona"], ["countryName": "Japan", "continent": "Asia", "population": "126672000", "currency": "Yen"], ["countryName": "Oman", "continent": "Middle East", "population": "4424762", "currency": "Rial"], ["countryName": "South Africa", "continent": "Africa", "population": "57725600", "currency": "South African Rand"], ["countryName": "Uruguay", "continent": "South America", "population": "3444006", "currency": "Uruguayan Peso"], ["countryName": "Venezuela", "continent": "South America", "population": "31568179", "currency": "Bolivar Soberano"]]
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela
cellTitle: Afghanistan
cellTitle: Albania
cellTitle: Bulgaria
cellTitle: Finland
cellTitle: Iceland
cellTitle: Japan
cellTitle: Oman
cellTitle: South Africa
cellTitle: Uruguay
cellTitle: Venezuela

我怎样才能只循环一次这个数组,在每个实例中选择“countryName”的值,然后将其用作我的单元格标题?

感谢任何能提供帮助的人。这个让我快疯了!

标签: iosswiftuitableviewdictionary

解决方案


您似乎误解了表视图的工作原理。

cellForRowAt每行调用一次countries次数)。您必须获取传递的数据源数组中的值indexPath并更新 UI:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Country", for: indexPath)
    let country = countries[indexPath.row]
    cell.textLabel?.text = country["countryName"]!
    return cell
}

最有效的解决方案是使用Codable协议将 JSON 解码为结构(并丢弃 SwiftyJSON)

struct Country : Decodable {
    let countryName, continent, currency : String
    let population : Int
}

var countries = [Country]()

override func viewDidLoad() {
    super.viewDidLoad()

    title = "Select country"

    // If the code crashes in one of the following lines you made a design mistake
    let jsonURL = Bundle.main.url(forResource: "Countries", withExtension: "json")!
    let data = try! Data(contentsOf: jsonURL)
    countries = try! JSONDecoder().decode([Country].self, from: data)
    tableView.reloadData()
}

...

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Country", for: indexPath)
    let country = countries[indexPath.row]
    cell.textLabel?.text = country.countryName
    return cell
}

推荐阅读