Swift - Как я могу использовать словарь, чтобы дать текст для ячеек tableView? - PullRequest
0 голосов
/ 24 августа 2018

У меня есть следующий файл 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 голосов
/ 24 августа 2018

Метод tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) вызывается один раз для каждой ячейки, которую необходимо создать в табличном представлении.

Это означает, что в вашей реализации вам не нужно зацикливать свои данные, а скорее возвращать однуэлемент, который запрашивает табличное представление прямо сейчас.Параметр indexPath указывает, для какой строки вы должны вернуть ячейку.

Поскольку ваши данные представляют собой словарь, возможный способ справиться с этим - сохранить ключи в виде массива, а затем использовать свойство indexPath.row для доступа к ключу, соответствующему строке.

0 голосов
/ 24 августа 2018

Вы перебираете весь массив для каждой строки

Вместо

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
0 голосов
/ 24 августа 2018

Хорошо, почему вы перебираете все элементы внутри ячейки для строки по пути индекса.Смысл в том, чтобы использовать индексный путь в массиве как 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()
        }
    }

}
0 голосов
/ 24 августа 2018

Вы, похоже, неправильно понимаете, как работают представления таблиц.

cellForRowAt вызывается один раз для каждой строки (число countries раз).Вы должны получить значение в массиве источника данных для переданного indexPath и обновить пользовательский интерфейс:

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
}

Наиболее эффективное решение - декодировать JSON в структуры с помощью Codableпротокол (и выбросить 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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...