Right CellIdentifier for the cell in UITableView

Go To StackoverFlow.com


Can someone explain the difference between

static NSString* CellIdentifier = @"Cell";


NSString *CellIdentifier = [NSString stringWithFormat: @"Cell%i", indexPath.row];

When should I use the first one and where the second?

2012-04-04 17:11
by Buron
Can you provide some context? Where did you see these - Josh Caswell 2012-04-04 17:15
Second one is better in any case as all the cells are ought to have its own unique Cell Identifier as per its indexPath.row - Parth Bhatt 2012-04-04 17:20
I had cells with such context: image and text. I used the first the type of cellidentifier. As a result tableview didn't work properly, cells mixed sometimes when i scrolled the tableview(i mean that one cell was copied 3 or 2 times on the screen), then i had changed cellIdentifier to the second type, and tableview worked properly - Buron 2012-04-04 17:21
@Buron An identifier should identify a type of cell which can then be customized based on the content of that particular row. If you're getting cells more than once or with the wrong content then you have something setup incorrectly - mydogisbox 2012-04-04 17:50
@Buron if you have a specific question not answered by my answer, then it would probably be better to ask a new question since that isn't covered by your current question - mydogisbox 2012-04-04 17:56
@ParthBhatt that is not true, the point of re-using cells is for performance, and you do so by having cell identifiers. If you have multiple cells that are the same you can re-use the previously allocated cell and update what you need to such as labels or images on the cell. This provides a huge performance gain being that all new cells do not need to be allocated when the cells come into view, which can happen very quickly with tables as users can scroll very fast - Chris Wagner 2012-04-04 17:59


static NSString* CellIdentifier = @"Cell";

This identifier (assuming there are no others) will identify a pool of cells from which all rows will pull when they need a new cell.

NSString *CellIdentifier = [NSString stringWithFormat: @"Cell%i", indexPath.row];

This identifier will create a pool of cells for each row, i.e. it will create a pool of size 1 for each row, and that one cell will always be used for only that row.

Typically you will want to always use the first example. Variations on the second example like:

NSString *CellIdentifier = [NSString stringWithFormat: @"Cell%i", indexPath.row % 2];

would be useful if you want every other row to have a certain background color or something of that sort.

An example of how to properly setup cell creation from here:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"mainTitleKey"];
    cell.detailTextLabel.text = [item objectForKey:@"secondaryTitleKey"];
    NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"imageKey"] ofType:@"png"];
    UIImage *theImage = [UIImage imageWithContentsOfFile:path];
    cell.imageView.image = theImage;

    return cell;
2012-04-04 17:48
by mydogisbox
Thanks for the excellent explanation - Buron 2012-04-04 18:00