2017-10-09 38 views
0

我正在下载并从服务器上用AlamofireImage显示缩略图,下载工作正常,但是当我滚动tableivew单元格图像时,我一直在搜索并找不到任何解决方案,例如prepareForReuse在自定义的手机中。这里是我的代码:用Alamofire显示桌面单元格图像的问题

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TrendingCell 

     let trendingIndex = trendingArray[indexPath.row] as! [String:Any] 

     if let media = trendingIndex["thumbnails"] as? [String:Any] { 
      if let thumbnail = media["medium"] as? [String:Any] { 
       thumbnailURL = String(describing:thumbnail["url"]!) 
      } 
     } 



     Alamofire.request(thumbnailURL).responseImage { response in 
      if let image = response.result.value { 
       cell.thumbnail.image = image 
      } 

     } 

     return cell 
    } 

如何避免图像更改时,用户滚动tableview?

+0

https://stackoverflow.com/questions/46199203/downloading-uiimage-via-alamofireimage/46199246#46199246 –

+0

我会建议使用[SDWebImage](https://github.com/rs/SDWebImage)库的图像 – Srdjan

+1

您必须将其写入单元格imageView –

回答

1

正如萨尔曼Ghumsani提到here:当您滚动表视图图像并没有改变

//Download and set thumbnail 
     if let imageURL = URL(string: thumbnailURL), let placeholder = UIImage(named: "Default") { 
      cell.thumbnail.af_setImage(withURL: imageURL, placeholderImage: placeholder) //set image automatically when download compelete. 
     } 

现在:

我们可以使用AlamofireImage扩展设置一个默认的缩略图像这样。

+0

接受答案 – Vinodh