gpt4 book ai didi

json - 处理多页 JSON 和 TableView?

转载 作者:行者123 更新时间:2023-11-30 12:54:42 25 4
gpt4 key购买 nike

我想将练习列表导入到表格 View 中。我可以导入第一页,但这只是少数练习。 JSON 似乎有“下一页”链接,我希望能够在开始加载过程时加载它们。

如果连续页面存在,我是否需要使用某种 if else 语句来加载它们,否则会结束加载?

这是我的 API 服务

open class ApiService: NSObject {

open func getData(completionHandler: @escaping (NSDictionary?, NSError?) -> Void) -> Self {

let requestUrl = "https://wger.de/api/v2/exercise/?format=json"

Alamofire.request(requestUrl, method: .get, encoding: URLEncoding.default)
.responseJSON { response in
switch response.result {
case .success( let data):

completionHandler(data as? NSDictionary, nil)

case .failure(let error):
print("Request failed with error: \(error)")
completionHandler(nil, error as NSError?)
}
}
return self
}

}

这是我的 JSON,它的第一行清楚地引用了以下页面

https://wger.de/api/v2/exercise/?format=json

这是我的表格 View ,如果相关则将数据拉入

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
if (searchActive){
cell.textLabel?.text = filtered[indexPath.row]
} else {
if let row: NSDictionary = arrRes[indexPath.row] as NSDictionary? {

guard let name = row["name"] as? String else {
print("Fail")
return cell
}
cell.textLabel?.text = name
}
}
return cell
}

感谢您的一些建议!

最佳答案

您可以在后台加载下一页练习,然后在请求完成时将它们添加到结果数组(在您的情况下是 arrRes)。

然后,当你打电话时tableView.reloadRows(at: [IndexPath], with: UITableViewRowAnimation) 与新结果的索引, TableView 应该为新结果设置动画。

关于json - 处理多页 JSON 和 TableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40563996/

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