I think you’ve made the classic table view image loading mistake: you don’t account for the fact that table view cells are re-used on scrolling.

A table view can can use the same table view cell instance for different index paths (so long as they are not both on screen at the same time). If an image is not available for a cell then the API call is started to load it. Meanwhile the user scrolls so far through the table that the cell goes off the top and is re-used on the bottom. If the first API call completes it will set the image to the table view cell. But that cell is now being used for a different row!

After updating your cache you need to get the table view cell again by asking the table view for the cell at the indexPath you are loading. That will be nil if that row is no longer on the screen. If it is not nil, then use it to set the image.

It is not safe to keep a reference to the cell dequeued from the table view and then use that to set the image.

Written by

Independent and freelance software developer for iPhone, iPad and Mac

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store