Swift 在TableViewCell中加载图像
我想将图像从url加载到productimage,但我的代码不起作用。Swift 在TableViewCell中加载图像,swift,uitableview,Swift,Uitableview,我想将图像从url加载到productimage,但我的代码不起作用。 如何将图像加载到表格视图单元格 public func tableView(\utableview:UITableView,cellForRowAt indexath:indexPath)->UITableView单元格{ 让cell=self.tableView.dequeueReusableCell(标识符为:“cellWomanSaved”)作为!WomanSavedTableViewCell let数据:数据模型 D
如何将图像加载到
表格视图单元格
public func tableView(\utableview:UITableView,cellForRowAt indexath:indexPath)->UITableView单元格{
让cell=self.tableView.dequeueReusableCell(标识符为:“cellWomanSaved”)作为!WomanSavedTableViewCell
let数据:数据模型
Data=DataList[indexPath.row]
cell.pricellabel.text=数据.Price
cell.productLabel.text=Data.ImageURL
cell.productImage.image=UIImage(数据:url)
如果让url=url(字符串:productLabel[indexath.row]){
做{
let data=try data(contentsOf:url)
self.swipeImage.image=UIImage(数据:数据)
}捕捉错误{
打印(“错误:\(Error.localizedDescription)”)
}
}
返回单元
}
更改此行
if let url = URL(string: productLabel[indexPath.row]) {
到
您可以使用SDWebImage库从URL轻松加载图像并在imageView中显示。SDWebImage非常方便,因为它可以下载并缓存图像,下次当您需要来自同一URL的图像时,它会从缓存中加载图像 它真的很容易使用。您可以通过cocoapod、carthage或手动将库代码添加到项目中 以下是github回购协议的链接:
- 在单元格中创建数据任务
- 在prepareforeuse函数中取消数据任务
- 将url传递给控制器cellForRowAt函数中的单元格
- 因此,使用模型填充单元格并将值设置为属性
class MyCell: UITableViewCell { // MARK: - Outlets @IBOutlet weak var priceLabel: UILabel! @IBOutlet weak var productLabel: UILabel! @IBOutlet weak var productImage: UIImageView! /// create dataTask for cancel in prepareForReuse function private var dataTask: URLSessionDataTask? /// Like this override public func prepareForReuse() { super.prepareForReuse() dataTask?.cancel() } func populate(with model: YourModel) { priceLabel.text = model.Price productLabel.text = model.ImageUrl /// You should set url in indexPath of your logo array([ImageUrl]) let url = model.ImageUrl /// It's sample url for explain this is an url of your current index model if let imageUrl = url { downloaded(from: imageUrl) } } func downloaded(from url: URL, contentMode mode: UIView.ContentMode = .scaleAspectFit) { contentMode = mode dataTask = URLSession.shared.dataTask(with: url) { data, response, error in guard let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200, let mimeType = response?.mimeType, mimeType.hasPrefix("image"), let data = data, error == nil, let image = UIImage(data: data) else { return } DispatchQueue.main.async() { self.productImage.image = image } } dataTask?.resume() } }
let model: DataModel = DataList[indexPath.row]
cell.populate(with: model)
return cell
在代码中,您可以在下载之前指定图像。声明之前使用的变量“url”。此代码是否成功生成并运行?如果让url=url(字符串:productLabel[indexPath.row]),则cell.productImage.image=UIImage(数据:url)。addingPercentEncoding(使用AllowedCharacterSet.UrlQueryLowed)!{do{let data=try data(contentsOf:url)self.swipeImage.image=UIImage(data:data)}catch let error{print(“error:(error.localizedDescription)”)}不工作..因为cell.productimage.imgae找不到let url,let data/try data也有一个错误。你能修复它吗?
let model: DataModel = DataList[indexPath.row]
cell.populate(with: model)
return cell