【问题标题】:How to show JSON response in tableview according to segment in swift如何在 swift 中根据段在 tableview 中显示 JSON 响应
【发布时间】:2021-07-04 15:54:01
【问题描述】:

我在视图控制器中调用了树段。 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 requestsArrayto show in tableview.. but now thisrequestsArraywant 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 响应

【问题讨论】:

    标签: json swift tableview segment


    【解决方案1】:

    创建 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

    【讨论】:

    • 谢谢,一个问题是..最初在索引 0 处,tableview 没有显示..如果我转到第一个索引并返回索引 0,那么 tableview 正在显示
    • 我也在 didload 中重新加载了 tableview
    • 您必须确保表视图正在主线程上重新加载。
    • 是的,仅在主线程中我正在重新加载.. 但最初不显示如果我移动到第一个索引并达到 0 索引然后 tableview 显示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多