2017-10-09 77 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