我已經決議了一個 JSON 檔案,現在我正在嘗試創建一個帶有部分的表格視圖。該部分是資料模型中的字典。
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]
現在我正在嘗試創建我的表格視圖部分,但我收到以下錯誤:“[ActionElement]”型別的值沒有成員“actionGoal”
兩個問題:我該如何解決這個問題?什么是了解更多關于資料模型和表視圖的好資源?
這是我正在撰寫的代碼:
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
}
uj5u.com熱心網友回復:
我相信您需要索引資料,然后將原始值(字串)用作ActionGoal列舉。
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return result?.data[section].actionGoal.rawValue
}
uj5u.com熱心網友回復:
您忘記訪問陣列元素
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return result?.data[section].actionGoal.rawValue
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/459734.html
