views:

55

answers:

2

I wonder why I need a Cell Identifier in a UITableView... like this:

static NSString *cellIdentifier = @"Cell";

what's that needed for? Example?

+1  A: 

It's used as a key for caching of cells, for example:

- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString * CellIdentifier    = @"MyCell1";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   ...

Then a different table might use a different identifier...

Colin
A: 

Cell Identifiers are good if you need to reuse cells to conserve application memory. For example, if you have many cells in your application, instead of releasing the cell once the user scrolls past it, the cell is simply modified to contain the info of new cells that you are creating. This conserves space since it isn't necessary to allocate thousands of cells if the user is only looking at 10 at a time. The identifier is what the system uses to check if there are cells with that identifier in existence already. If there are some, it uses them. Otherwise, it must allocate new space and create new objects.

JCoop