我正在学习斯威夫特。我有一个问题。
问题- 我有带有图像 url 的 DataModel,所以第一次会从 url 下载图像,当然第二次不会。因此,当我在我的块中获取图像时,我想用图像更新我的数据模型。但它不起作用。
- 我试过 inout 功能
- 还有completionHander回调
ViewController.swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OffersTableCell", for: indexPath) as! OffersTableCell
var model = offersArray[indexPath.row] as! OffersModel
cell.updateOfferCellWith(model: model, completionHandler: { image in
model.image = image
})///// I am getting image here, but model is not getting update
return cell
}
细胞.斯威夫特
func updateOfferCellWith(model: OffersModel, completionHandler:@escaping (UIImage) -> Void) {
if (model.image != nil) { //Second time this block should be execute, but model image is always nil
DispatchQueue.main.async { [weak self] in
self?.offerImageView.image = model.image
}
}
else{
//First time- Download image from URL
ImageDownloader.downloadImage(imageUrl: model.offerImageURL) { [weak self] image in
DispatchQueue.main.async {[model] in
var model = model
self?.offerImageView.image = image
//model.image = image*//This is also not working*
completionHandler(image)*//Call back*
}
}
}
}