gpt4 book ai didi

ios - swift 5 : Display Data Retrieved in Closure to Table View

转载 作者:行者123 更新时间:2023-12-01 21:59:20 25 4
gpt4 key购买 nike

我已经在函数的闭包中成功地从我的 API 获取了一些数据。我需要将数据显示到 Storyboard上的表格中。

我知道它不显示的原因是因为数据保存在闭包中,除非我有完成处理程序,否则我无法从外部访问它。我在 Stack Overflow 上阅读了许多其他问题,但我无法真正理解。我也尝试过重新加载表,但它只返回“意外发现 nil”的错误。

声明属性

class ClaimProperties {
var id: Int
var date: String
var status: String

init(id: Int, date: String, status: String) {
self.id = id
self.date = date
self.status = status
}
}

仪表板 Controller
struct Claims: Decodable {
let id: Int
let submission_date: String
let status: String
init(json: [String:Any]) {
id = json["id"] as? Int ?? -1
submission_date = json["submission_date"] as? String ?? ""
status = json["status"] as? String ?? ""
}
}

class DashboardController: UIViewController, GIDSignInUIDelegate {

@IBOutlet weak var tableView: UITableView!
var tempArray: [ClaimProperties] = []

override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}

// getTokenFromAPI() gets called form AppDelegate.swift once the user logs in via Google API

func getTokenFromAPI(usersAppToken:String) {
guard let urlString = URL(string: "https://claim.ademo.work/claims/") else { return }
var requestAPI = URLRequest(url: urlString)

requestAPI.httpMethod = "GET"
requestAPI.addValue("application/json", forHTTPHeaderField: "Content-Type")
requestAPI.addValue("application/json", forHTTPHeaderField: "Accept")
requestAPI.setValue("Bearer \(usersAppToken)", forHTTPHeaderField: "Authorization")

URLSession.shared.dataTask(with: requestAPI) {
(data, response, error) in
if let data = data {
do {

let json = try JSONDecoder().decode([Claims].self, from: data)

for n in 0..<json.count {
self.tempArray.append(ClaimProperties(id: json[n].id, date: json[n].submission_date, status: json[n].status))
}

// This is the data I'm trying to display -> tempArray

} catch let error {
print("Localized Error: \(error.localizedDescription)")
print("Error: \(error)")
}
}
}.resume()
}
}

extension DashboardController: UITableViewDataSource, UITableViewDelegate {

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ClaimCell", for: indexPath) as! AllMyClaimsTable
cell.idField.text = String(tempArray[indexPath.section].id)
cell.dateField.text = tempArray[indexPath.section].date
cell.statusField.text = tempArray[indexPath.section].status
return cell
}
}

总之,我要做的只是将 API 数据显示到我的表格 View 中。

最佳答案

移动您的reload在 for 循环下的行

URLSession.shared.dataTask(with: requestAPI) { [weak self] (data, response, error) in
if let data = data {
do {

let json = try JSONDecoder().decode([Claims].self, from: data)

for n in 0..<json.count {
self?.tempArray.append(ClaimProperties(id: json[n].id, date: json[n].submission_date, status: json[n].status))
}
self?.tableView.reloadData()

} catch let error {
print("Localized Error: \(error.localizedDescription)")
print("Error: \(error)")
}
}
}.resume()

您需要在循环结束后重新加载,在 tempArray 之后被完全填满。

关于ios - swift 5 : Display Data Retrieved in Closure to Table View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60520176/

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