3

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;
}
Scott
  • 1,154
  • 1
  • 12
  • 25
  • 2
    Your code will work fine. But if your table has more than a few rows it is far less efficient. – rmaddy Apr 22 '14 at 17:00
  • Can you explain why? I was under the impression that reuseIdentifier needed to alloc a new cell for each cell presented as well? – Scott Apr 22 '14 at 17:01
  • 1
    Yes, but just enough to fill the screen. Once the user starts scrolling, those cells get reused. In your code, you would keep creating new cells. – rmaddy Apr 22 '14 at 17:02
  • Ah hah! I figured there was a simple purpose! Much appreciated, I'll accept your answer. – Scott Apr 22 '14 at 17:04
  • Thanks but I didn't post an answer. – rmaddy Apr 22 '14 at 17:06

3 Answers3

5

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.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    this is the right answer. Without it you get jerky motion of table scroll. – Sam B Apr 22 '14 at 17:10
  • rmaddy explained in the question comments that once the users starts scrolling that those cells are reused and no more are allocated. I think that's something very important to note as well too that's not included on this very well explained answer. – Scott Apr 22 '14 at 17:13
  • Ah, never mind, I was actually thinking of previous questions I've see where the op creates an array of cells and uses that to store all of the cells. Anyway, question... Are you blinkenlights that did the ASCII Star Wars stream? – Fogmeister Apr 22 '14 at 17:32
  • @Fogmeister No, I picked the name almost randomly. PS: the ASCII Star Wars animation is amazing! – Sergey Kalinichenko Apr 22 '14 at 17:39
3

Your current method creates a new cell each time, using reuseIdentifier allows the table view to dynamically reuse views to save on memory allocation.

Your code will probably work, but it's not allowing your tableView to be as efficient as it could be. Particularly as your rows increase in number.

Logan
  • 52,262
  • 20
  • 99
  • 128
0

As it was written before, it's more efficient to use reusable cells. Your app will create only a few cells instead of as many cells as the count of your array (or whatever you want to display).

But please note that, if you reuse a cell that was set up before, you should redefine the content of it.

For example if you want to display Book titles and subtitles in a cell, and you set up a cell with the contents, and you reuse that cell, but that time there is no subtitle for the book, the cell will display the previous subtitle you had set up earlier, unless you clear it.

danieltmbr
  • 1,022
  • 12
  • 20