How to dequeue UITableViewCells

Sometimes Xcode does a bit of magic behind the scenes which I don’t really understand.

Consider this standard table view method to display cells:

Where do we actually ever create a UITableViewCell? All we’re asking here is “do we have reusable ones available” – but if the answer is “no”, then somehow these get created without an error message. Other times (such as when using an overlay table view courtesy of the search display controller) we get an error message.

This brings me to the point that perhaps Xcode and iOS create as well as dequeue cells automatically – but when they don’t, here’s how we can do this manually.

Let’s amend the above code snippet:

Now we properly create a cell should a dequeueable one not be available.

I haven’t had to use this, and the if statement never seems to get called if inserted into the Master/Detail template, yet I find this may be something to keep in mind for future projects.

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.