gpt4 book ai didi

ios - 对 TableView 的每个单元格的 Alamofire 请求

转载 作者:行者123 更新时间:2023-11-28 15:07:16 25 4
gpt4 key购买 nike

由于多个 Alamofire 请求,我想通过解析收到的 JSON 数据来填充表格 View 。然而,问题是每个单元格都有与其他单元格不同的请求。每个单元格根据 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
}

如何实现每一个请求?提前致谢!

最佳答案

您应该跟踪每个请求的 Hook 索引路径,以便您知道返回的值是什么

 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:跟踪正在进行的下载

关于ios - 对 TableView 的每个单元格的 Alamofire 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48265404/

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