【问题标题】:Swift: Complex JSON Model data not properly showing in UITableViewSwift:复杂的 JSON 模型数据未正确显示在 UITableView 中
【发布时间】:2020-05-27 01:13:31
【问题描述】:

我有一个复杂的 JSON(嵌套值),我正确实现并且数据值在模型中。但是由于一些未知的原因,它没有打印完整的数据,只首先显示12 values,实际上它有71 data values。 我知道由于数据复杂,我在调整 indexPath 时做错了。我的模型和 json 是复杂的(嵌套的)和迭代之一。

  1. 我需要在选择列表(检查 json)和textField 值列表中显示仅具有特定喜欢的 tableView 数据 - 操作类型 actionType": 101title 作为部分。
  2. 那么我怎样才能正确地设置AppData?.items?[indexPath.row] 这个部分和行。

注意:我只需要 JSON 中的一种操作类型,即 101,Title 作为 tableView 部分,textField 作为列表值,这些都在 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 
}

【问题讨论】:

    标签: ios json swift uitableview decodable


    【解决方案1】:

    你需要

    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 }
    

    【讨论】:

    • 谢谢,但问题是我无法更改模型结构,因为它与我用于其他模型的结构相同。那可能吗 ?但是项目模型中的所有字段都是optionals,但将来可能会出现
    • 您不必更改名称,您必须更改要解码的数据的层次结构
    • 好的,让我实现它,我怎样才能显示 tableView section ?目前我的var AppData: SectionList? 现在将更改为var AppData: Root? 对吗?
    • 我已根据您提到的答案实施并更改了层次结构,但只得到一个section 和一个row。你能检查我更新的问题新代码添加吗?我如何获得所有部分和行?我知道我做错了什么
    猜你喜欢
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多