2017-09-22 73 views
0

嗨我下載圖像,然後在tableViewCell中設置,但當我滾動圖像改變,一段時間後,他們得到正確設置這不是真的好用戶這是我的代碼在視圖控制器tableViewCell:滾動時圖像改變

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if let cell = tableView.dequeueReusableCell(withIdentifier: "cellZ") as? ProductCell { 
     let proCell = service.productArr[indexPath.row] 
     cell.cellConfigure(cellProduct: proCell) 
     return cell 
    } 
    return ProductCell() 
} 

,這是我的UITableViewCell(ProductVC)代碼

override func prepareForReuse() { 
    super.prepareForReuse() 
    self.imageProduct.image = UIImage(named: "sumGray") 
} 

func cellConfigure(cellProduct : product) { 
    productName.text = cellProduct.title 
    productFeature.text = cellProduct.info[0].capitalized 

    let urlImage = URL(string: cellProduct.pic)! 
    DispatchQueue.global().async { 
     do { 
      let img = try Data(contentsOf: urlImage) 
      DispatchQueue.global().sync { 
       self.imageProduct.image = UIImage(data: img) 
      } 
     } catch let err as NSError { 
      print(err.debugDescription) 
     } 
    } 
} 

}

我加了複用的功能,但它只會變得更糟,用C掛在ImageView的默認圖片 林搜查計算器我找到像重複使用一些方法,但他們並沒有解決我的問題 我想看到當前圖像的每一個細胞 感謝幫助

回答

0

嘗試以下操作:

  • 開始下載之前,請保存URL並在下載過程完成時檢查URL是否未更改(由於信元重用)。只有在URL沒有變化的情況下才使用下載的圖片。
  • 不要從後臺線程UI代碼,設置下載的圖像時,你應該做的:

    DispatchQueue.main.async { 
        // if the URL didn't change 
        self.imageProduct.image = UIImage(data: img) 
    } 
    
+0

我應該改變我的調度代碼,此代碼 – MahdiM