【发布时间】:2016-11-12 14:45:58
【问题描述】:
我想将练习列表导入到表格视图中。我可以导入第一页,但这只是少数练习。 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,它在第一行明确引用了以下页面
这是我的表格视图,如果相关,则将数据拉入
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
}
感谢您的建议!
【问题讨论】:
标签: json swift uitableview api alamofire