从 JSON 文件的数据模型创建 table 视图部分
Create table view section from a data model from a JSON file
我已经解析了一个 JSON 文件,现在我正在尝试创建一个带有部分的 table 视图。该部分是数据模型中的字典。
import Foundation
struct ActionResult: Codable {
let data: [ActionElement]
}
struct ActionElement: Codable {
let actionID: Int
let actionItem: String
let actionGoal: ActionGoal
let actionImage: String
let actionBenefit: ActionBenefit
let actionSavings: Int
let actionType: ActionType
let actionDescription, actionTips: String
let actionInformationURL: String
let actionSponsorURL: String
enum CodingKeys: String, CodingKey {
case actionID = "ActionID"
case actionItem = "ActionItem"
case actionGoal = "ActionGoal"
case actionImage = "ActionImage"
case actionBenefit = "ActionBenefit"
case actionSavings = "ActionSavings"
case actionType = "ActionType"
case actionDescription = "ActionDescription"
case actionTips = "ActionTips"
case actionInformationURL = "ActionInformationURL"
case actionSponsorURL = "ActionSponsorURL"
}
}
enum ActionBenefit: String, Codable {
case costs = "Costs"
case education = "Education"
case environment = "Environment"
case health = "Health"
}
enum ActionGoal: String, Codable {
case cleanEnergy = "Clean Energy"
case cleanWater = "Clean Water"
case climateAction = "Climate Action"
case economicGrowth = "Economic Growth"
case goodHealth = "Good Health"
case noPoverty = "No Poverty"
case promoteEquality = "Promote Equality"
case qualityEducation = "Quality Education"
case responsibleConsumption = "Responsible Consumption"
case zeroHunger = "Zero Hunger"
}
enum ActionType: String, Codable {
case sticky = "Sticky"
case todoe = "Todoe"
}
typealias Action = [ActionElement]
现在我正在尝试创建我的 table 视图部分,但出现以下错误:“[ActionElement]”类型的值没有成员 'actionGoal'
两个问题:我该如何解决这个问题?什么是了解更多关于数据模型和 table 视图的好资源?
这是我正在编写的代码:
var result: ActionResult?
var index = 0
override func viewDidLoad() {
super.viewDidLoad()
parseJSON()
}
// Table View Sections
override func numberOfSections(in tableView: UITableView) -> Int {
return result?.data.count ?? 0
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return result?.data.actionGoal
}
您忘记访问数组元素
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return result?.data[section].actionGoal.rawValue
}
我相信您需要索引数据,然后使用原始值(字符串),因为 ActionGoal
是一个枚举。
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return result?.data[section].actionGoal.rawValue
}
我已经解析了一个 JSON 文件,现在我正在尝试创建一个带有部分的 table 视图。该部分是数据模型中的字典。
import Foundation
struct ActionResult: Codable {
let data: [ActionElement]
}
struct ActionElement: Codable {
let actionID: Int
let actionItem: String
let actionGoal: ActionGoal
let actionImage: String
let actionBenefit: ActionBenefit
let actionSavings: Int
let actionType: ActionType
let actionDescription, actionTips: String
let actionInformationURL: String
let actionSponsorURL: String
enum CodingKeys: String, CodingKey {
case actionID = "ActionID"
case actionItem = "ActionItem"
case actionGoal = "ActionGoal"
case actionImage = "ActionImage"
case actionBenefit = "ActionBenefit"
case actionSavings = "ActionSavings"
case actionType = "ActionType"
case actionDescription = "ActionDescription"
case actionTips = "ActionTips"
case actionInformationURL = "ActionInformationURL"
case actionSponsorURL = "ActionSponsorURL"
}
}
enum ActionBenefit: String, Codable {
case costs = "Costs"
case education = "Education"
case environment = "Environment"
case health = "Health"
}
enum ActionGoal: String, Codable {
case cleanEnergy = "Clean Energy"
case cleanWater = "Clean Water"
case climateAction = "Climate Action"
case economicGrowth = "Economic Growth"
case goodHealth = "Good Health"
case noPoverty = "No Poverty"
case promoteEquality = "Promote Equality"
case qualityEducation = "Quality Education"
case responsibleConsumption = "Responsible Consumption"
case zeroHunger = "Zero Hunger"
}
enum ActionType: String, Codable {
case sticky = "Sticky"
case todoe = "Todoe"
}
typealias Action = [ActionElement]
现在我正在尝试创建我的 table 视图部分,但出现以下错误:“[ActionElement]”类型的值没有成员 'actionGoal'
两个问题:我该如何解决这个问题?什么是了解更多关于数据模型和 table 视图的好资源?
这是我正在编写的代码:
var result: ActionResult?
var index = 0
override func viewDidLoad() {
super.viewDidLoad()
parseJSON()
}
// Table View Sections
override func numberOfSections(in tableView: UITableView) -> Int {
return result?.data.count ?? 0
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return result?.data.actionGoal
}
您忘记访问数组元素
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return result?.data[section].actionGoal.rawValue
}
我相信您需要索引数据,然后使用原始值(字符串),因为 ActionGoal
是一个枚举。
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return result?.data[section].actionGoal.rawValue
}