如何将json对象的值设置为swift 3中tableView中动态呈现的视图中的标签?

Posted

技术标签:

【中文标题】如何将json对象的值设置为swift 3中tableView中动态呈现的视图中的标签?【英文标题】:How to set json object's value to labels in views rendered dynamically in tableView in swift 3? 【发布时间】:2017-06-29 11:27:07 【问题描述】:

我想将键 name 设置为视图中的标签。问题是视图基于 json 响应中的对象。例如。 places 中有两个对象,这将在一行中呈现两个视图,它们具有名称标签。因此,如果我们按列查看,Pune 将在第 1 列,Mumbai 在第 2 列(numberOfSections 仅为 1,我只是在谈论 UI 视图)。现在 id=3 在places 中只有 1 个对象,它只会呈现标签名称为“Surat”的单个视图,因此如何根据 json 中收到的对象将数据设置为标签。我用过tableView。已创建两个 xib 以在表格行中动态呈现单视图和双视图。请帮忙!!。这个问题花了很多时间。

[
  
    "id": 1,
    "places": [
      
        "state": "Maharashtra",
        "name": "Pune"
      ,
      
        "state": "Maharashtra",
        "name": "Mumbai"
      
    ]
  ,
  
    "id": 2,
    "places": [
      
        "state": "Punjab",
        "name": "Amritsar"
      ,
      
        "state": "Punjab",
        "name": "Pathankot"
      
    ]
  ,
  
    "id": 3,
    "places": [
      
        "state": "Gujarat",
        "name": "Surat"
      
    ]
  
]

【问题讨论】:

【参考方案1】:

你的 JSON 结构是

数组 -> 字典 -> 数组 -> 字典

因此,假设您有此 JSON 对象的单视图单元格和双视图单元格。首先,计算顶层数组中有多少对象,它会告诉您表格视图中需要多少个单元格。所以你的代码看起来像这样

var topArray: [Dictionary<String, Any>]!

override func viewDidLoad() 
    super.viewDidLoad()

    let jsonStr = "[\"id\": 1,\"places\": [\"state\": \"Maharashtra\",\"name\": \"Pune\",\"state\": \"Maharashtra\",\"name\": \"Mumbai\"],\"id\": 2,\"places\": [\"state\": \"Punjab\",\"name\": \"Amritsar\",\"state\": \"Punjab\",\"name\": \"Pathankot\"],\"id\": 3,\"places\": [\"state\": \"Gujarat\",\"name\": \"Surat\"]]"
    let data = jsonStr.data(using: .utf8)!
    do 
        topArray = try JSONSerialization.jsonObject(with: data, options: []) as? [Dictionary<String, Any>]

    catch 
        print("something goes wrong")
    



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    return topArray.count

然后,在您的 cellForRow 方法中,首先计算较低级别数组中有多少项,以确定您正在使用哪个单元格。然后拼命对付他们。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    if let lowerArray = topArray[indexPath.row]["places"] as? [Dictionary<String, String>] 
        if lowerArray.count == 1 
            let cell = tableView.dequeueReusableCell(withIdentifier: "singleCell") as! SingleCell

            cell.textField.text = lowerArray[0]["name"]

            return cell
         else 
            let cell = tableView.dequeueReusableCell(withIdentifier: "doubleCell") as! DoubleCell

            cell.textField1.text = lowerArray[0]["name"]
            cell.textField2.text = lowerArray[1]["name"]

            return cell
        

     else 
        return nil //Do your handling
    


【讨论】:

方明你好……你能详细解释一下完整的json解析和UI设置值吗?由于我是 swift 新手,我发现它很困难。我试过了,但我得到了 lowerLevelArray 的重复值.....请帮助。 我在这一行遇到错误......如果 lowerArray.count = 1 (它说不能分配属性:'count is get only parameter')如果我这样做,如果 lowerArray.count ==1 然后它在所有 3 行上打印 Pune Mumbai。 你得到正确的输出了吗?我正在使用 xcode 8.2.1,swift 3 @279 抱歉更新了我的答案。所以我之前的回答是强制读取 JSON 中的第一个元素。现在看到我正在阅读来自indexPath 喂方明……!!非常感谢.....!!!它起作用了......只是做了一些小的改变......而不是topArray [0]做topArray [indexPath.row]和lowerArray.count == 1......它完成了......谢谢很多

以上是关于如何将json对象的值设置为swift 3中tableView中动态呈现的视图中的标签?的主要内容,如果未能解决你的问题,请参考以下文章

Swift 4 将字符串解析为 json 对象

将 SWIFT 字典替换为 JSON 对象

如何将 json 数据从 alamofire 转换为 swift 对象

我可以使用镜像设置Swift对象属性的值吗?

如何将模型对象转换为 JSON - Swift 4

Swift json 解析错误:无法将 NSCFConstantString 类型的值转换为 NSArray