如何根据 swift 中的段在表视图中显示 JSON 响应
How to show JSON response in tableview according to segment in swift
我调用了 viewcontroller 中的树段。 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 this
requestsArraywant to show in
open` 段就像关闭段中相同的关闭值..怎么做
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
}
}
请帮我确定 JSON tableviewview 中的响应
创建 4 个数组
var openItems = [AppliedRequestCellData]()
var closedItems = [AppliedRequestCellData]()
var allItems = [AppliedRequestCellData]()
var items = [AppliedRequestCellData]()
items
是主数据源数组。
对OPEN、CLOSED、ALL的数据进行解码,赋值给对应的数组
在 indexChangeBlock
中更改主数组的内容并重新加载 table 视图
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
我调用了 viewcontroller 中的树段。 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 this
requestsArraywant to show in
open` 段就像关闭段中相同的关闭值..怎么做
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
}
}
请帮我确定 JSON tableviewview 中的响应
创建 4 个数组
var openItems = [AppliedRequestCellData]()
var closedItems = [AppliedRequestCellData]()
var allItems = [AppliedRequestCellData]()
var items = [AppliedRequestCellData]()
items
是主数据源数组。
对OPEN、CLOSED、ALL的数据进行解码,赋值给对应的数组
在 indexChangeBlock
中更改主数组的内容并重新加载 table 视图
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