IPhone - What are reuseIdentifiers (UITableViewCell)?

From the official documentation:

The reuse identifier is associated with a UITableViewCell that is created by the table view delegate with the intention of reusing it as the basis (for performance reasons) for several rows of the table view. It is assigned to the cell object in initWithFrame: reuseIdentifier: and after that it cannot be changed. The UITableView object maintains a queue (or list) of cells that can be reused, each with its own reuse identifier, and makes them available to the delegate in the dequeueReusableCellWithIdentifier: method.

http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/reuseIdentifier

I do not understand this. Well, I understand the basic idea, I think that you are creating UITableViewCells and trying to use as much as you can, instead of creating new ones (or something like that). But what exactly decides whether the cell can be reused? If I have two identical (visually) cells, but with different texts (well, I suppose they are not completely identical), can they have the same identifier? Or should they have different? Or in what situation should you use different identifiers?

Can someone clarify or relate to the place where he is?

+50
objective-c iphone uitableview reuseidentifier
Jan 28 '10 at 3:50
source share
3 answers

Ok, here is how I think this works:

Using dequeueReusableCellWithIdentifier for tableView, you can significantly speed up the process. Instead of creating an instance of many cells, you simply create an instance as many as necessary, i.e. As many as visible (this is processed automatically). If you scroll to an area in the list where there are "cells" that have not yet received their visual representation, instead of creating new ones, you reuse existing ones.

You can try it yourself by following these steps:

static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; NSLog(@"new one"); } else { NSLog(@"old one"); } 

Remember that you want dequeueReusableCellWithIdentifier to return the cell, if applicable. Therefore, if the cell is to be reused, make sure that it is correct for the situation. What are reuseIdentifiers for? Usually you only need one. But there may be a list that uses several different types of cells, in which case you will have to keep them separate, providing different reuseIdentifiers. Otherwise, you can get the cell that you consider as some other cell (for example, the UITableView cell, and not the one you wanted).

Basically, as I understand it, use different reuseIdentifiers for different types of cells, where kind means class. If you use only standard cells, you probably only need one re-identifier.

This design pattern is known as a combination of objects .

+65
Feb 15 '10 at 23:36
source share

Just add some things to quano otherwise a very good answer: (I tried to add this as a comment, but it was too long!)

Even with reuse, reuse identifiers may be omitted, although this should be done in special circumstances. If you have a table view of 6-7 cells, and each of them is different, you may find that creating a new cell with zero, as an identifier may be preferable.

The presence of a reusable cell means that every time cellForRowAtIndexPath is called, you must check the cell, initialize it if there is no reusable cell, and outside the init area you must explicitly iterate over all possible index paths and set values ​​for each label explicitly depending on of which cell you have! Thus, in a table view with 10 dinstinct cells, you will need to take care of creating the cell, if nil, and populate it depending on what you created.

Therefore, in this case, from the point of view of code maintenance, it is preferable to initialize each cell with the identifier nil (since it will not be reused) and fill in each cell information properly, without worrying about reuse.

+10
Aug 17 2018-12-12T00:
source share

A UITableView is like having a cell pool for each reuseIdentifier , so that it recycles the cell

I like this video from http://oleb.net/blog/2014/05/scrollviews-inside-scrollviews/

http://im.ezgif.com/tmp/ezgif-3302899694.gif

+2
Apr 16 '16 at 21:14
source share



All Articles