gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 11:49:41 25 4
gpt4 key购买 nike

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

感谢任何能提供帮助的人。这个让我抓狂!

最佳答案

你正在为每一行遍历整个数组

代替

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

关于ios - Swift - 如何使用字典为 tableView 单元格提供文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52003860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com