Why create a reuseIdentifier instead of alloc/init a cell inside tableView?

ios, uitableview

Solution

It's the speed of your scrolls: reusing cells helps you to a smooth scroll of table cells.

Calling `[[UITableViewCell alloc] init]` slows things down for no good reason. It is harder to notice for do-nothing cells, the heavier the cell becomes, the less smooth very quickly. Pre-allocating and reusing cells let you get a scroll that is visually pleasing.

In addition, throw-away CPU cycles drain your battery. A user can scroll through a table at a reasonably fast rate, so the cycles that you burn there can add up quickly.

Adding a reuse identifier eliminates calls to `[[UITableViewCell alloc] init]` beyond the few cells which are visible on the screen. When a cell goes off the screen, `UITableView` adds that cell to the pool of cells available for reuse, eliminating memory allocation and deallocation for the instance of the cell itself. When your `tableView:cellForRowAtIndexPath:` asks for the new cell, `UITableView` hands you back an instance that has been scrolled of the screen, so all you need to do is to re-configure the instance to look like the cell that you need to display. In many cases, reconfiguring an existing cell is significantly faster than allocating a new cell.

Here is a link to a question with information on other things that you could do to improve the performance of your scrolls.

Problem

I have an array of NSStrings stored as `self.array`. I am new to iOS and I know that reuseIdentifier most likely has a simple advantage that is eluding me but why go through all of that trouble and long syntax when the following works as well? ``` - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] init]; cell.textLabel.text = self.array[indexPath.row]; return cell; } ```

Original source

Related problems