我有一个复杂的JSON(嵌套值(,我正确实现了并且数据值在模型中。但是由于一些未知的原因,它没有打印完整的数据,只显示我的第一个12 values
,实际上它已经71 data values
了。 我知道由于复杂的数据,我在调整indexPath
时做错了什么。我的模型和 json 很复杂(嵌套(,迭代为一。
- 我需要显示具有特定像 - 操作类型
actionType": 101
和title
作为选项列表(检查 json(中的部分的 tableView 数据,并在 tableView 列表中显示textField
值。 - 那么我怎样才能正确地为部分和行设置
AppData?.items?[indexPath.row]
。
注意:我只需要来自 JSON 的一种操作类型,即 101 和标题作为表视图部分和文本字段作为列表值,这些都在 pickList 中。我附上了示例小 JSON。
法典:
var AppData: SectionList?
let decoder = JSONDecoder()
let response = try decoder.decode(SectionList.self, from: pickResult)
self.AppData = response
表视图:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return AppData?.items?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let dic = AppData?.items?[indexPath.row].actionType
return cell
}
型:
struct SectionList : Codable {
let title : String?
var items : [Item]?
var modified: Bool? = false
}
struct Item : Codable {
let actionType : Int?
let actionUrl : String?
let bgColor : String?
let booleanValue : Bool?
var textField : String?
var textValue : String?
let unitId : Int?
let latitude : Double?
let longitude : Double?
let actionParamData: String?
let actionTitle: String?
let pickList: [SectionList]?
var selection: [Item]?
let multiSelect: Bool?
let selectedValue: [String]?
let version: Int?
let masterId: Int?
let actionId: Int?
let itemValue: String?
var required: Bool? = false
}
示例 JSON:
{
"items": [
{
"actionType": 101,
"version": 3,
"pickList": [
{
"title": "Sayaç yeri seçimi",
"items": [
{
"textField": "Sayaç Yeri Seçiniz",
"itemValue": "0"
},
{
"textField": "Sayaç daire girişinde",
"itemValue": "1"
},
{
"textField": "Sayaç apt. girişinde",
"itemValue": "2"
},
{
"textField": "Sayaç bodrumda",
"itemValue": "3"
},
{
"textField": "Sayaç çatı katında",
"itemValue": "4"
},
{
"textField": "Sayaç bahçede (Müstakil)",
"itemValue": "5"
},
{
"textField": "Sayaç bina dışında",
"itemValue": "6"
},
{
"textField": "Sayaç balkonda",
"itemValue": "7"
},
{
"textField": "Sayaç daire içinde",
"itemValue": "8"
},
{
"textField": "Sayaç istasyon içinde",
"itemValue": "9"
}
]
}
]
},
{
"actionType": 1015,
"version": 3,
"pickList": [
{
"title": "AĞAÇ KURUTMA ÜNİTESİ",
"items": [
]
}
]
},
{
"actionType": 1016,
"version": 3,
"pickList": [
{
"title": "ASTAR FIRINI",
"items": [
]
}
]
}
]
}
更新的代码
var AppData: [Inner]?
let decoder = JSONDecoder()
let response = try decoder.decode(Root.self, from: pickResult)
let res = response.items.filter { $0.actionType == 103 }
self.AppData = res
func numberOfSections(in tableView: UITableView) -> Int {
return AppData?.count ?? 0
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return AppData?[section].pickList[section].title
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return AppData?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let dic = AppData?[indexPath.section].pickList[indexPath.row].items
print(dic)
let data = dic?[indexPath.row].textField
cell.textLabel?.text = data
return cell
}
你需要
struct Root: Codable {
let items: [Inner]
}
struct Inner: Codable {
let actionType, version: Int
let pickList: [PickList]
}
struct PickList: Codable {
let title: String
let items: [PickListItem]
}
struct PickListItem: Codable {
let textField, itemValue: String
}
let decoder = JSONDecoder()
let response = try decoder.decode(Root.self, from: pickResult)
let res = response.items.filter { $0.actionType = 101 }