In my application I'm downloading some images using the URL and put them in the cell.imageView[indexPath.row]
(it's a custom cell with an imageView inside).
I can see two cells at a time and I noticed that the method
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> CustomCell {
}
It's called every time the cell will be displayed. So my app continue to download the image from the server and the app result very slow.
I would like to know if it's possible to cache the data of the cells I already saw, so I don't need to re-download the image if I scroll up in my tableView! Code speaking I would like to call the func
written above only if I scroll down the table and not for the cells I already saw.
Do not reinvent the wheel!
Take a look at NSCache, it works like a mutable dictionary but it thread safe.
You can key your images using their URL and them as a value.
Documentation.
NSHipster post.
You can but it takes a fair bit of work so the nice people at SDWebImage make a library to download / cache / fetch / manage your image URLs just swap your image getting code with this category for
UIImageview
, this givesUIImageView.sd_
methods.Also with cells implement
prepareForReuse()
and cancel the fetch withsd_cancel