ios - 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”的值,然后将其用作我的单元格标题?
感谢任何能提供帮助的人。这个让我快疯了!
解决方案
您似乎误解了表视图的工作原理。
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
}
推荐阅读
- c# - 如何从水晶报表“数据集报表”的设计者创建的报表中捕获水晶报表 SQL?
- javascript - 有条件地添加 babel core-js 的正确方法是什么
- java - Springboot本机查询不给出结果
- xamarin - 在 Xamarin.Android 中使用活动结果 API
- r - 有没有一种用数据表在 R 中制作堆叠条形图的好方法?
- unity3d - 在移动浏览器上玩 Unity webGl 游戏时如何保存游戏数据?
- r - 有没有办法使用 ggpmisc::stat_poly_eq 将观察次数添加到绘图标签
- javascript - 多按钮在html webform中不起作用,当单击显示按钮时它正在提交
- javascript - 尝试使用 Bootstrap 日期选择器时出错
- reactjs - 如何使用 setInterval 中的值更新外部变量?