Firebase
을 사용하여 이미지를 내 셀 ImageView
에로드하지만! 문제가있다. reusable cells
로드 할 때 이미지를 redownload 방지하기 위해 캐싱 메커니즘을 사용하고 있지만 내 TableView
스크롤 할 때까지 이미지가로드되지 않습니다.TableView 이미지가 스크롤 될 때까지로드되지 않습니다.
다음은 코드입니다.
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
cell.imgv.image = nil
if let urlString = self.filteredFlats[indexPath.row].flatThumbnailImage?.imageDownloadURL
{
imageURLString = urlString
let url = URL(string: urlString)
if let imagefromCache = imageCache.object(forKey: urlString as AnyObject) as? UIImage
{
DispatchQueue.main.async {
cell.imgv.image = imagefromCache
return
}
}
else
{
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
DispatchQueue.main.async {
let imagetoCache = UIImage(data:data!)
if self.imageURLString == urlString
{
cell.imgv.image = imagetoCache
}
self.imageCache.setObject(imagetoCache!, forKey: urlString as AnyObject)
}
}).resume()
}
}
return cell
}
나는 URLSession
이미지를 다운로드 ImageView
로를 설정할 때 문제가, 생각하지만 위로 스크롤 아래로 할 때로드 된 이미지는 다시라는 indexpath.row
때까지 표시되지 않습니다.
나는 이것이 if 문이라는 것을 깨달았습니다.
if self.imageURLString == urlString
하지만 제거하면 셀이 재사용 가능한 셀의 동작이 악화됩니다. 일부 셀은 이전 캐시 된 이미지를 계속 표시합니다.
실제로 무슨 일이 일어나고 있는지 알지 못합니다. 기대하지 않았던 것은 그 자체로 복잡한 구조 인 TableViews
입니다.
감사합니다.
는 이미지 뷰의 확장을 만들려면 다음과 같이
그리고 사용자 정의 셀 클래스 파일에
, 우리는 그것을 사용할 수 있습니다 이 URL을 사용하여 : http://stackoverflow.com/a/37019507/3400991 당신을 도울 희망 –