您能帮我吗
我正在尝试使用Alamofire从API中提取数据。是否可以在同一请求中提取诸如[“ title”]和Image之类的简单数据,并将其填充到表格视图中?
这是我的代码:
Alamofire.request(baseURL).responseJSON { (response) in
switch response.result {
case .success:
if let value = response.result.value{
let json = JSON(value)
json.forEach({ (temp) in
let title = temp.1["title"].stringValue
let run = temp.1["run"].stringValue
let image = temp.1["thumb"].stringValue
let newmodel = Schedule(title: title, run: run, image: image)
self.scheduleArray.append(newmodel)
})
}
case .failure(let error):
print(error.localizedDescription)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
答案 0 :(得分:0)
您可以先获取基本数据,然后再获取单个请求的每张图片
使用以下代码获取基本数据:
func POSTWithLoad(urlString: String,paramaters: [String: AnyObject]? = nil,success:@escaping (_ responseObject: AnyObject?) -> Void ,failure: @escaping (_ error: String?) -> Void ) {
Alamofire.request(baseUrlDev+urlString, method: .post, parameters:paramaters, encoding: URLEncoding.default, headers: headers).responseJSON(completionHandler: { (responseData) in
if (responseData.result.error == nil && responseData.result.isSuccess == true) {
//success
success(responseData)
} else {
//fauiler
failure("UnKnowen Data")
}
})
}
并将其用于getImage:
func getImage(link : String , completion : @escaping (_ imageWithLink : (link : String,image : UIImage)) -> Void){
var image = UIImage(named: "placeholder")
URLSession.shared.dataTask(with: NSURL(string: link.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)! as URL, completionHandler: { (data, response, error) -> Void in
if error != nil {
print("errorLoadIamge : "+link)
//completion(image)
print(error)
return
}
DispatchQueue.main.async(execute: { () -> Void in
let mimage = UIImage(data: data!)
if(mimage != nil){
image = mimage!
completion((link : link , image : image))
print("successLoadIamge : "+link)
return
}
})
}).resume()
}
然后可以像这样在您的ViewContoller中使用它:
let products : [Product] = []
let productImages : [String] = []
POSTWithLoad(urlString: "YApiLink" , paramaters: requestParameters as [String : AnyObject]?, success: { (successObject) in
products = Mapper<Product>().mapArray(JSONObject : successObject)
productsImages = []
print(successObject)
}, failure :{ (failureObject) in
print(failureObject)
}
)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let product = products[indexPath.row]
let cell = YourTableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) as! YourTableViewCell
if let imageAndLink = productsImages.first(where: { $0.link == cell.product?.productImage})
{cell.pImage?.image = imageAndLink.image}
else{ cell.pImage.image = UIImage(named: "placeholder")
getImage(link: cell.product?.productImage ?? "", completion: {(newImage) in
DispatchQueue.main.async {
if self.productsImages.first(where: { $0.link == newImage.link}) == nil
{
self.productsImages.append(ProductImage(link : newImage.link , image : newImage.image) )
}
if cell.product?.productImage == newImage.link{
cell.pImage.image = newImage.image
}
}
})
}
}