我在视图控制器中有树段被调用。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 i am getting "open" values and adding them in段下面,类似于close segment..how中相同的关闭值。
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响应
发布于 2021-04-08 18:33:10
创建4个数组
var openItems = [AppliedRequestCellData]()
var closedItems = [AppliedRequestCellData]()
var allItems = [AppliedRequestCellData]()
var items = [AppliedRequestCellData]()items是主要的数据源数组。
将数据解码为打开、关闭和全部,并将它们分配给相应的数组。
在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,它提供了一个很好的变化动画。
还可以考虑使用Codable和JSONDecoder
https://stackoverflow.com/questions/67009732
复制相似问题