简体   繁体   English

表格视图的每个单元格的Alamofire请求

[英]An Alamofire request for each cell of a table view

I want to populate a table view by parsing JSON data received thanks to several Alamofire requests. 我想通过解析由于几个Alamofire请求而收到的JSON数据来填充表视图。 However, the thing is that each cell has a different request from the others. 但是,问题是每个单元格的请求都与其他单元格不同。 Each cell makes a request based on IndexPath. 每个单元格都基于IndexPath发出请求。 Since the calls are asynchronous, not all requests are achieved... 由于调用是异步的,因此并非所有请求都可以实现...

My code: 我的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    var name = ""
    let url = "https://........"+items[indexPath.section][indexPath.row]+"......"

    Alamofire.request(url).responseJSON { (responseData) -> Void in
        if((responseData.result.value) != nil) {
            let swiftyJsonVar = JSON(responseData.result.value!)
            let object = swiftyJsonVar["results"][0]
            print(object)
            name = object["name"].stringValue
            cell.textLabel?.text = name
        }
    }
    return cell
}

How would it be possible to achieve every request? 如何实现每个请求? Thanks in advance! 提前致谢!

You should keep track of hooking indexpath with every request so you know what the value returned is for 您应该跟踪每个请求的挂钩索引路径,以便知道返回的值是

 struct Item
{
    var indexPath:NSIndexPath!

  func getData(index:NSIndexPath)
  {

      let url = "https://........"+items[indexPath.section][indexPath.row]+"......"

      Alamofire.request(url).responseJSON { (responseData) -> Void in
    if((responseData.result.value) != nil) {
        let swiftyJsonVar = JSON(responseData.result.value!)
        let object = swiftyJsonVar["results"][0]

         globalArr// store indexpath and value

         /// remove indexoath from allDownloads

         /// send refresh to table

      }
     }

  }

}

in cell for row 在单元格中

     if(indexPath in globalArr)
      {
         // show it
      }
      else
      { 

           // check whether it's download is in progress to avoid multiple same requests when scroll because of dequeuing 
         if(allDownloads doesn't contain inexpath)
         {

              // create object of item and start download

              // allDownloads.add(object)

          }

       }

globalArr: keep track of all downloaded objects globalArr:跟踪所有下载的对象

allDownloads : keep track of in progress downloads allDownloads:跟踪正在进行的下载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM