Cell Reuse

Right now, we create a new UITableViewCell in every call to tableView(cellForRowAtIndexPath:). If we flick through a really long table, that might mean we create a cell that will only appear for an instant before it goes off the screen and is no longer needed. As it turns out, creating views is fairly expensive, so if we can avoid doing that frequently, it will make our app faster and more responsive.

The UITableView class is actually built to cache and reuse cells. It provides a method, dequeueReusableCellWithIdentifier, that takes a string identifying a cell to be reused. The idea is that we can create a cell in the storyboard as a sort of template and identify it with a known string. In code, we’ll ask for a cell by this name. ...

Get iOS 9 SDK Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.