Swift-如何使用字典为tableView单元格提供文本?

时间:2018-08-24 11:50:09

标签: ios swift uitableview dictionary

我有以下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”的值,然后将其用作单元格标题?

感谢任何可以提供帮助的人。这一直让我发疯!

4 个答案:

答案 0 :(得分:1)

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

每行

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
}

答案 1 :(得分:1)

您要遍历每一行的整个数组

代替

var cellTitle = ""

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

cell.textLabel?.text = cellTitle

尝试

cell.textLabel?.text = countries[indexPath.row]["countryName"] as! String

答案 2 :(得分:1)

表视图需要创建的每个单元格都被调用一次tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)方法。

这意味着在您的实现中,您不必遍历数据,而只需返回表视图当前要求的一项。参数indexPath指示您应为哪一行返回单元格。

由于您的数据是字典,因此可能的处理方式是将其键存储为数组,然后使用属性indexPath.row来访问与该行相对应的键。

答案 3 :(得分:-1)

为什么在索引路径中的行中循环遍历单元格内的所有项目。重点是将数组上的索引路径用作countries[indexPath.row]["countryName"]

但是有些其他事情可能会有点...有点奇怪。也许从研究以下改造开始。我已经删除了映射,并使用了本机工具来解析JSON,以使其目前更加独立。

class ViewController: UITableViewController {

    private var countries: [[String: Any]]?

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Select country"
        reloadData()
    }

    private func reloadData() {
        if let jsonPath = Bundle.main.path(forResource: "Countries", ofType: "json") {
            if let data = FileManager.default.contents(atPath: jsonPath) {

                if let stringRepresentation = String(data: data, encoding: .utf8) {
                    print("Data: \(stringRepresentation)")
                } else {
                    print("Could not represent data as string (\(data.count) bytes) ")
                }


                if let countries = (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)) as? [[String: Any]] {
                    self.countries = countries
                    print("countries: \(countries)")
                } else {
                    print("Could not parse countries")
                }

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

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let descriptor = self.countries?[indexPath.row] {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Country", for: indexPath)
            cell.textLabel?.text = descriptor["countryName"] as? String
            return cell
        } else {
            // No data!?
            return UITableViewCell()
        }
    }

}

但是就映射而言,至少拥有自己的结构是一件好事。之后,Swift提供了一些不错的工具,例如compactMap,它将把一种对象类型的数组转换为另一种。请参见以下代码:

class ViewController: UITableViewController {

    struct Country {
        let name: String
        let continent: String?
        let population: Int
        let currency: String?

        init?(descriptor: [String: Any]) {
            guard let name = descriptor["countryName"] as? String else {
                return nil // We want at least the country name to be mandatory
            }
            self.name = name
            self.continent = descriptor["continent"] as? String
            self.population = (descriptor["population"] as? Int) ?? 0
            self.currency = descriptor["currency"] as? String
        }
    }

    private var countries: [Country]?

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Select country"
        reloadData()
    }

    private func reloadData() {
        if let jsonPath = Bundle.main.path(forResource: "Countries", ofType: "json") {
            if let data = FileManager.default.contents(atPath: jsonPath) {

                if let stringRepresentation = String(data: data, encoding: .utf8) {
                    print("Data: \(stringRepresentation)")
                } else {
                    print("Could not represent data as string (\(data.count) bytes) ")
                }


                if let countries = (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)) as? [[String: Any]] {
                    self.countries = countries.compactMap { Country(descriptor: $0) }
                    print("countries: \(countries)")
                } else {
                    print("Could not parse countries")
                }

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

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

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

}