首页 > 解决方案 > 如何在 swift 中根据段在 tableview 中显示 JSON 响应

问题描述

我在视图控制器中有树段调用。OPEN, CLOSED, ALL

我的 JSON 响应如下:

{
"jsonrpc": "2.0",
"result": {
    "data": {
        "open": [
            {
                "user_id": "10",
                "request_title": "Title-2",
                "category": "4",
                "gender": "M",
                "location": "On earth",
                "from_date": "2021-04-09",
             }
             {
            "user_id": "10",
            "request_title": "Title-2",
            "category": "4",
            "gender": "M",
            "location": "On earth",
            "from_date": "2021-04-09",
            }
            ........
            ]
        "close": [
            {
                "user_id": "10",
                "request_title": "Title-2",
                "category": "4",
                "gender": "M",
                "location": "On earth",
                "from_date": "2021-04-09",
             }
            ........
            ]
        "all": [
            {
                "user_id": "10",
                "request_title": "Title-2",
                "category": "4",
                "gender": "M",
                "location": "On earth",
                "from_date": "2021-04-09",
             }
             {
            "user_id": "10",
            "request_title": "Title-2",
            "category": "4",
            "gender": "M",
            "location": "On earth",
            "from_date": "2021-04-09",
            }
            ........
            ]
    }
}
}

在这里,我能够获得 JSON 响应.. 并且在i am getting "open" values and adding them in requestsArray to show in tableview.. but now thisrequestsArray want to show inopen` 段下方,就像关闭段中的相同关闭值一样。如何做到这一点

     if let code = ((resp.dict?["result"] as? [String : Any])){
               
                let totalData = code["data"] as? [String : Any]
                if let open = totalData?["open"] as? [[String : Any]]{
                    for (value) in open {
                        self?.title_req = value["request_title"] as? String
                        self?.gender = value["gender"] as? String
                        self?.location = value["location"] as? String
                                                    
            self?.requestsArray.append(AppliedRequestCellData(request_title: self?.title_req, gender: self?.gender, location: self?.location))
                        
            }
        DispatchQueue.main.async {
        self.tableView.reloadData()
    }                }

我的部分如下

          hmSegment.indexChangeBlock = { index in
        print("in index segment")
        print(index)
        if index == 0{
          // here how to show JSON `open` response in tableview
           }

        if index == 1{
        // here how to show JSON `close` response in tableview
        }
     if index == 2{
       // here how to show JSON `all` response in tableview
     }


}

请帮我用段在 tableviewview 中鞋 JSON 响应

标签: jsonswifttableviewsegment

解决方案


创建 4 个数组

var openItems = [AppliedRequestCellData]()
var closedItems = [AppliedRequestCellData]()
var allItems = [AppliedRequestCellData]()

var items = [AppliedRequestCellData]()

items是主要的数据源数组。

解码 OPEN、CLOSED 和 ALL 的数据并将它们分配给相应的数组。

indexChangeBlock更改主数组的内容并重新加载表视图

hmSegment.indexChangeBlock = { index in
    print("in index segment")
    switch index { 
       case 0: self.items = self.openItems
       case 1: self.items = self.closedItems
       case 2: self.items = self.allItems
       default: break
    }
    self.tableView.reloadData()
}  

考虑使用UITableViewDiffableDataSource它提供了一个很好的变化动画。
还考虑使用CodableJSONDecoder


推荐阅读