views:

348

answers:

3

I am puzzled at the following line "static NSString *MyIdentifier = @"MyIdentifier";" in the method: cellForRowAtIndexPath

What does that line do? Is it just creating a random pointer to an NSString object and assigning it the string? Why is it being called MyIdentifier, I have seen this in many examples.

#import "AddToFavorites.h"


@implementation AddToFavorites

- (id)initWithStyle:(UITableViewStyle)style {
  if (self = [super initWithStyle:style]) {
  }
  return self;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}


- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section {
  return 5;
}


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

static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView 
dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
reuseIdentifier:MyIdentifier] autorelease];
}
// Configure the cell

return cell;
}

@end

Here is another example, this one has a different string, CellIdentifier.

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

static NSString *CellIdentifier = @"TimeZoneCell";

UITableViewCell *cell = [tableView 
dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
 cell = [self tableviewCellWithReuseIdentifier:CellIdentifier];
}

[self configureCell:cell forIndexPath:indexPath];
return cell;
}
+4  A: 

UITableViews can automatically reuse cells to save on memory. To take advantage of this, you must specify a "reuse identifier" which is used by the UITableView to be able to look up existing cells ("dequeueReusbaleCellWithIdentifier") with the same identifier as the one you will create if it can't find an existing cell.

The line creates a static variable (global in that it is shared by all code paths and only initialized once, but local in that you can only access it in this method) to hold the NSString for the identifier. My guess is that this is to ensure that the same pointer is used every time, as comparing pointers is quick and easy, while comparing the contents of strings can take a little bit longer.

NilObject
A: 

For performance as mentioned but also to get help from the compiler in catching spelling errors. There is no checking of your identifier if you use a @""-string literal. The compiler will error out you if you misspell the static identifier. Also codesense will autocomplete the static identifier.

monowerker
A: 

The identifier is a key or tag that allows you to have multiple separate collections of cells for different purposes.

This saves you time and RAM memory - let's find out how.

Let's suppose you had a contacts list application, with two types of contacts, businesses and friends.

If you wanted to display these differently, then you might design two types of cell - one with a picture (friend photo) and name in black font, and one with just the name of the company and no picture or icon.

When the user is using the application, it might need to display 3 friends and 4 companies with names starting with "A-M" at first, so it needs 3 friend cells and 4 company cells. You pass it these, and tag all the friend cells with the identifier "friend", and all the business ones with the identifier "business".

When later on the view changes and just wants names starting with "P-T", you might just have 7 businesses. Ideally you would re-use the cells you already created, so it requests 7 cells with identifier "business", and it turns out you already tagged 4 cells that you already created with "business", so it simply re-uses those. The remaining 3 you already created have the wrong tag, so it ignores those (or maybe deletes them?) and creates 3 new business type cells and gives them the tag "business".

By re-using cells in this way you save on Memory (only need as many cells can be displayed at once of each type), and Performance (no need to go to the effort of allocating and initialising new cells while scrolling up and down). You trade this off against the additional programmer effort of writing this selection code and giving things ids.

They could have automatically tagged cells based upon the objective-C type, but this wouldn't work if you programmatically created the contents of a cell rather than subclassing or using the Interface builder to lay out your cells. So they provide the identifier mechanism instead.

If you only have one type of cell in your table, just call it "Alice" and forget about it.

Alex Brown