【发布时间】:2020-05-27 18:57:27
【问题描述】:
我正在尝试在 UITableView 中实现部分和行,但由于嵌套的 JSON 模型结构,我没有成功。它总是只打印first section title。我正在使用Codable 方法并且模型结构无法更改。
如何在tableView 中实现部分和行?任何指导或帮助将不胜感激。我真的为此而苦苦挣扎。
- 我需要在 tableView 部分显示
title和 RowstextField-- 请参阅 JSON。 - 附上只打印一张的截图
section title
型号:
struct SectionList : Codable {
let title : String?
var items : [Item]?
}
struct Item : Codable {
let actionType : Int?
var textField : String?
let pickList: [SectionList]?
let itemValue: String?
let version: Int?
}
初始化&TableView代码:
var AppData: [Item]?
let decoder = JSONDecoder()
let response = try decoder.decode(SectionList.self, from: pickResult)
let res = response.items?.filter { $0.actionType == 101}
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[0].title
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return AppData?[section].pickList?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let dic = AppData?[indexPath.section]//.pickList?[indexPath.row].title
//AppData?[indexPath.section].pickList[indexPath.row].items
//print(dic)
return cell
}
【问题讨论】:
标签: ios json swift uitableview