ios - Alamofire 多个请求

标签 ios swift alamofire

我有 tableView 并使用 Alamofire 从服务器发送和获取请求。 正如您在图片中看到的,在我的 tableView 中,我在表头和索引 0,1 处的 cell 发送多个请求。如果我在一个函数中发送请求,那么获得响应的时间就会太长,并且无法控制每个请求的错误。处理这个问题的最佳方法是什么?

谢谢

最佳答案

您应该创建一个请求

因此,通过解析您的请求,您应该将数据源设置为数组,这样每一行都会从单个项目获取数据> 该数组的。

然后您将使用此数据源更新表格 View 。

例如:

1) 为数据源(方法之外)创建变量:

var source : [Something]

2) 在单个请求中请求所有内容

Alamofire.request(_,_,_)
.responseJSON {
   // create a data source
   // this request should be a merge of your three request stated in your question
   // source can be set as [Something] for instance
}

3)在您的cellForRow委托(delegate)方法中,您应该执行以下操作:

func tableView(_ tableView: UITableView, 
  cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
   let cell = tableView.dequeueReusableCell( ... 
   dataForARow = self.source[indexPath.row]
   cell.label.text = dataForARow["name"] //etc ...
}

关于ios - Alamofire 多个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39846500/

相关文章:

swift - 使用 alamofire 发送 base64 字符串

ios - 无法存档 Xcode 项目,错误没有这样的模块 'Alamofire'

ios - 调用 Swift Xcode 7 中的 canEvaluatePolicy 额外参数 'error'

ios - swift 。 Viewcontroller 知道子类

ios - 配置应用程序时出现 Firebase 错误 - 无法识别的选择器已发送至类

ios - 在 Swift 的另一个类中声明 self

ios - 如何在 Alamofire 中请求带有正文和 header 的 JSON?

ios - NSPredicate ANY 多列

swift - 减小使用 AVAssetExportSession 导出的视频的大小 - iOS Swift

swift 3传递数组中的Json