【问题标题】:Handling a multiple page JSON and TableView?处理多页 JSON 和 TableView?
【发布时间】: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,它在第一行明确引用了以下页面

https://wger.de/api/v2/exercise/?format=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


    【解决方案1】:

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

    然后,当您调用 tableView.reloadRows(at: [IndexPath], with: UITableViewRowAnimation) 带有新结果的索引,表格视图应该为新结果设置动画。

    【讨论】:

    • 听起来不错,但我不知道该怎么做,这是最初的问题
    【解决方案2】:

    与您在此处从 json 文件中获取 name 属性的方式相同

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

    无论您想在哪里加载下一页,都可以执行类似的操作

    // data is the json result you have from the first request
    func lookForNextPage(data: NSDictionary, completionHandler: @escaping (NSDictionary?, NSError?) -> Void) {
        // Try and see if a next page exist
        if let nextPage = data["next"] as? String {
            // There is a next page, fetch it and add result to datasource
            Alamofire.request(requestUrl, method: .get, encoding: URLEncoding.default)
                .responseJSON { response in
                    switch response.result {
                    case .success( let data):
                        // Handle the new data as you wish
                        completionHandler(data as? NSDictionary, nil)
    
                    case .failure(let error):
                        print("Request failed with error: \(error)")
                        completionHandler(nil, error as NSError?)
                    }
            }
        }
    }
    

    好吧,根据你的需要调整它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多