ios - 节和行未正确显示在UITableView Swift中

标签 ios json swift uitableview

我正在尝试在UITableView中实现节和行,但是由于嵌套的JSON模型结构,我无法成功。它总是只打印first section title。我正在使用Codable方法,并且无法更改模型结构。

如何在tableView中实现节和行?任何指导或帮助将不胜感激。我真的为此感到挣扎。

我需要在tableView中显示的

  • 部分为title和行textField-请参阅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 
    }
    

    最佳答案

    不管您有多了解,我都能做到。检查此代码

    将结构创建为

    struct SectionList : Codable {
    
        let title : String?
        var items : [RowItems]?
    
    }
    
    struct RowItems: Codable {
        var textField : String?
        let itemValue: String?
    }
    
    struct SourceData: Codable {
        let items: [Item]?
    }
    struct Item : Codable {
        let actionType : Int?
        let pickList: [SectionList]?
        let version: Int?
    
    }
    

    创建类似于的变量
    var AppData: Item?
    

    将json解析为
    let decoder = JSONDecoder()
                let response = try decoder.decode(SourceData.self, from: data)
                let res = response.items?.filter { $0.actionType == 101}
                print("jsonData:\(res)")
                AppData = res?.first
    

    call 表数据源为
    func numberOfSections(in tableView: UITableView) -> Int {
        return AppData?.pickList?.count ?? 0
        }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return AppData?.pickList?[section].title
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return AppData?.pickList?[section].items?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        let dic = AppData?.pickList?[indexPath.section].items?[indexPath.row]//.pickList?[indexPath.row].title
            //AppData?[indexPath.section].pickList[indexPath.row].items
        //print(dic)
    
        cell.textLabel?.text = dic?.textField
        return cell
    }
    

    使用此代码的屏幕截图
    Table after this changes

    关于ios - 节和行未正确显示在UITableView Swift中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60184596/

    相关文章:

    python - 如何检查 AlchemyAPI 中剩余的调用次数? Python

    javascript - 从 json 文件中检索数据

    ios - 如何按索引打印 JSON 字典元素?

    xcode - 在 Swift 中将 NSManagedObject 添加到 CoreData 对多关系时防止循环

    IOS保留一个viewcontroller

    ios - 更新 View 的框架保持约束

    java - 在 MongoDB 中存储多个 JSON 文档(具有嵌套 JSON 键值对象的行数组格式)

    ios - 如何用UIBezierPath删除徒手画?

    ios - 在 NSURLConnection 中使用 base64 字符串

    ios - AVPlayer,长时间停顿,然后恢复=故障