簡體   English   中英

表格視圖的每個單元格的Alamofire請求

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

我想通過解析由於幾個Alamofire請求而收到的JSON數據來填充表視圖。 但是,問題是每個單元格的請求都與其他單元格不同。 每個單元格都基於IndexPath發出請求。 由於調用是異步的,因此並非所有請求都可以實現...

我的代碼:

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
}

如何實現每個請求? 提前致謝!

您應該跟蹤每個請求的掛鈎索引路徑,以便知道返回的值是

 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

      }
     }

  }

}

在單元格中

     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:跟蹤所有下載的對象

allDownloads:跟蹤正在進行的下載

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM