我試圖做一個表格,當圖像完成加載(異步)時,單元格上的imageView將alpha從0更改爲1。 我做了什麼似乎只是將圖像顯示在一個而不是淡入。我確定它是某種競爭條件,但我是iOS新手,並且不知道如何解決這個問題。任何輸入都會很棒。 這裏是我的代碼:異步加載動畫cell.imageview
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
//Configure the cell...
let episode = episodes[indexPath.row]
cell.textLabel?.text = episode.title
cell.detailTextLabel?.text = episode.content
let logoUrl = URL(string: episode.logoUrl!)
if (episode.logoImage == nil){
episode.logoImage = UIImage()
DispatchQueue.global().async {
let data = try? Data(contentsOf: logoUrl!) //make sure your image in this url does exist, otherwise unwrap in a if let check/try-catch
DispatchQueue.main.async {
episode.logoImage = UIImage(data: data!)
cell.imageView?.image = episode.logoImage
self.episodesTable.reloadData()
cell.imageView?.alpha = 0
UIView.animate(withDuration: 1, animations: {
cell.imageView?.alpha = 1
})
}
}
} else{
cell.imageView?.image = episode.logoImage
}
return cell
}
是否需要'self.episodesTable.reloadData()?嘗試刪除它可能嗎? –