【问题标题】:How to display child JSON data in swift 4?如何在 swift 4 中显示子 JSON 数据?
【发布时间】:2018-04-09 13:26:13
【问题描述】:

我有一个 JSON

[ 
  {
  "image": "/images/Home.png",
  "name": "Magazine2",
  "clothe": [
      {
        "nameClothe": "dasdas",
        "imageClothe": "sdvd"
      }
    ]

  },
  {
  "image": "/images/Home.png",
  "name": "Magazine1",
    "clothe": [
      {
        "nameClothe": "dasdass",
        "imageClothe": "sregfd"
      }
      ]
  }
]

我有一个结构

struct HeroStats:Decodable {
    let image: String
    let name: String
    let clothe: [SecondTable]?
}

struct SecondTable: Decodable {
    let nameClothe: String
    let imageClothe: String
} 

在第一个 tableView 中,我看到“Magazine2”和“Magazine1”。在单击之前,在第一个 tableView 中,我想在第二个 tableView 中查看杂志的子元素,以便数据相关。例如-我点击"Magazine2",我可以看到"nameClothe"这个

"name": "Magazine2",
  "clothe": [
      {
        "nameClothe": "dasdas",
        "imageClothe": "sdvd"
      }
    ]

我怎样才能创建这个?对不起我的英语...

【问题讨论】:

  • tableView(Magazine1、Magazine2)可以显示主要数据,但不能显示Magazine1和Magazine2的子数据。
  • 你现在到底遇到了什么问题,你想在不同VoewController的不同TableVIew中显示你的孩子数据吗?
  • 您的Clothe 数组数据是否正确填充?在Herostate struct 里面。

标签: json swift tableview


【解决方案1】:

您正在使用[Herostate] 填充您的FirstTableView,现在实现UITableViewDelegate 方法
optional func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 以侦听您的FirstTableView 单元格的触摸事件,在该方法中为SecondTableView 准备您的数据并执行Segue那里。
例如:

optional func tableView(_ tableView: UITableView, 
     didSelectRowAt indexPath: IndexPath){
       self.heroesClothes = self.heroes[indexPath.row]
       self.performSegue(withIdentifier "YourSegueIdentifier": String, 
       sender: Any?)
}

之后在您的ViewControllers prepareForSegue 方法的传递数据到下一个ViewController
例如:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourNextViewControllersIdentifier" {
    if let destinationVC = segue.destination as? SecondViewController{
        destinationVC.heroesClothes = self.heroesClothes 
    }
}
}

谢谢你:)

【讨论】:

    【解决方案2】:
        struct HeroStat: Codable {
        let image: String
        let name: String
        let clothe: [SecondTable]?
    }
    
    struct SecondTable: Codable {
        let nameClothe: String
        let imageClothe: String
    }
    

    然后……

    let heroStats = try? JSONDecoder().decode([HeroStat].self, from: jsonData)
    

    【讨论】:

    • 我有这个 :) 我可以在 tableView 中显示主要数据(杂志 1,杂志 2),但我不能显示杂志 1 和杂志 2 的子数据。
    【解决方案3】:

    试试 JSON 解码器,你应该没问题。或者,您可以使用 Swifty JSON,但我认为 JSON 解码器更好。

            let json = """
    {
      "image": "/images/Home.png",
      "name": "Magazine2",
      "clothe": [
          {
            "nameClothe": "dasdas",
            "imageClothe": "sdvd"
          }
        ]
    
      }
    
    """.data(using: .utf8)!
    
    
      let decoder = JSONDecoder()
        do {
            let hero = try decoder.decode(HeroStats.self, from: json)
            print(hero.name)
            print(hero.clothe[0].imageClothe)
    
        } catch {
            print(error.localizedDescription)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-07
      • 2021-01-02
      相关资源
      最近更新 更多