ios - 在 tableview 单元格中显示旧图像
问题描述
我有一个包含电影海报图像的表格视图,但是当我上下滚动列表时,我注意到需要几秒钟才能出现正确的海报,或者我必须再滚动一点,直到出现正确的图像如下图所示
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MovieTableCell
let entity = listOfMovies[indexPath.row]
cell.movie = entity
cell.movieTitle.text = entity.title
cell.moviesReleaseDate.text = entity.releaseDate
cell.voteAverage.text = "\(entity.voteAverage)"
getMoviePoster(entity.backdropPath) { (image) in
cell.moviePoster.image = image
}
func getMoviePoster(_ imagePath: String , completion: @escaping (_ image: UIImage) -> Void ) -> Void {
webserviceManager().getMoviePoster(imagePath:imagePath) { (success, image) in
if success {
completion(image!)
}
}
}
func getMoviePoster(imagePath:String, completion: @escaping (_ success:Bool, _ image:UIImage?) -> Void ) -> Void {
if Connectivity.isConnectedToInternet {
let url = "https://image.tmdb.org/t/p/w500\(imagePath)"
Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseImage { (respones) in
if respones.data != nil {
let webRespones = respones.result.value
completion (true,webRespones)
} else {
completion (false,nil)
}
}
} else {
completion (false,nil)
}
}
解决方案
This is caused when you dequeue the cells fast, aka fast scrolling.
You can fix this simply by adding a default placeholder image or an empty one in your cell prepareForReuse()
function, something like this.
override func prepareForReuse() { // this is in my Custom Cell class
imageView.image = UImage(named: "myPlaceholder") // this is will be called whenever you dequeue the cell .
}
推荐阅读
- php - 如何在变量中使用制表符空间
- mysql - 对错误 1060“列名重复”感到困惑
- arrays - 使用 React Hook 将数据添加到对象内的 Array
- r - 用于引导 ChiSq 中值估计的 R 代码检查
- java - 使用 GridBagLayout 和 JScrollPane 创建 Instagram 类型的提要
- mysql - 在状态的发布请求中发送日期始终返回 NULL
- python - 使用给定的输入列表对 DataFrame 列进行排序
- powerbi - 如何在 power bi visuals 中显示当前日期和时间?
- angularjs - 使用搜索框导航到其他状态
- django - 如何自定义 django 管理员