首页 > 解决方案 > 在 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)

        }
    }

标签: iosswiftuitableview

解决方案


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 . 
   }

推荐阅读