Geoff Hackworth
1 min readMay 23, 2018

--

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.

--

--

Geoff Hackworth
Geoff Hackworth

Written by Geoff Hackworth

Independent and freelance software developer for iPhone, iPad and Mac

No responses yet