我有 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/