gpt4 book ai didi

json - 需要一些帮助在 POST 请求后使用 JSON 数据填充 tableview

转载 作者:行者123 更新时间:2023-11-30 10:42:31 24 4
gpt4 key购买 nike

本质上,我需要在发出 POST 请求并且 API 用 JSON 响应后在表格 View 中显示 JSON 数据。

我没有收到任何错误,但也没有收到调试器中显示的任何 JSON 响应。我不确定到底出了什么问题,但表格 View 中似乎没有加载或显示任何内容。

以下是我用来解释 JSON 的结构:

import UIKit

struct ScheduleStructure: Codable {
let customer: String
let PickedUpNUM: String
let DeliveryNUM: String

}

JSON 应该是这样的:

[
{
"customer": “Example”,
"PickedUpNUM": “2”,
"DeliveryNUM": “4”
}
]

这是当前的表格 View Controller :

import UIKit



class ScheduleCell: UITableViewCell {
@IBOutlet weak var cellStructure: UIView!
@IBOutlet weak var testingCell: UILabel!
@IBOutlet weak var pickupLabel: UILabel!
@IBOutlet weak var deliveryLabel: UILabel!
}

class ScheduleTableViewController: UITableViewController {

var driverName = UserDefaults.standard.string(forKey: "name")!


var structure = [ScheduleStructure]()

override func viewDidLoad() {
super.viewDidLoad()
UINavigationBar.appearance().isTranslucent = false

self.view.backgroundColor = UIColor.white


//Adds Shadow below navigation bar
self.navigationController?.navigationBar.layer.masksToBounds = false
self.navigationController?.navigationBar.layer.shadowColor = UIColor.lightGray.cgColor
self.navigationController?.navigationBar.layer.shadowOpacity = 0.8
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 2.0)
self.navigationController?.navigationBar.layer.shadowRadius = 2


self.extendedLayoutIncludesOpaqueBars = true
fetchJSON()
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(doSomething), for: .valueChanged)
tableView.refreshControl = refreshControl

}



private func fetchJSON() {
guard
let url = URL(string: "https://example.com/example/example.php"),
let value = driverName.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed)
else { return }

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = "value=\(value)".data(using: .utf8)

URLSession.shared.dataTask(with: request) { data, _, error in
DispatchQueue.main.async {
//
}
}.resume()

}

@objc func doSomething(refreshControl: UIRefreshControl) {
print("reloaded")

fetchJSON()

}



override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return structure.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


let cell = tableView.dequeueReusableCell(withIdentifier: "customerID", for: indexPath) as! ScheduleCell



cell.textLabel?.font = .boldSystemFont(ofSize: 18)
cell.textLabel?.textColor = UIColor.red

let portfolio = structure[indexPath.row]

cell.textLabel?.text = portfolio.customer


return cell

}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


let cell = tableView.dequeueReusableCell(withIdentifier: "customerID", for: indexPath) as! ScheduleCell
//cell.cellStructure.layer.cornerRadius = 50
let portfolio = structure[indexPath.row]


let navigationController = UINavigationController(rootViewController: controller)

self.present(navigationController, animated: true, completion: nil)

}


override func viewWillAppear(_ animated: Bool) {
fetchJSON()

}



}

如果有更好的方法,请告诉我

我只需要执行 JSON POST 并将结果显示在表格中。

最佳答案

您需要将 json 响应解码为 API 回调中的数组

URLSession.shared.dataTask(with: request) { data, _, error in 
guard let data = data else { return }
do {
self.structure = try JSONDecoder().decode([ScheduleStructure].self,from:data)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
catch {
print(error)
}

}.resume()
<小时/>
struct ScheduleStructure: Codable {
let customer, pickedUpNUM, deliveryNUM: String

enum CodingKeys: String, CodingKey {
case customer
case pickedUpNUM = "PickedUpNUM"
case deliveryNUM = "DeliveryNUM"
}
}

关于json - 需要一些帮助在 POST 请求后使用 JSON 数据填充 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56483741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com