I’m looking at some code and was wondering how this works. In one class, I see something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleCellIdentifier = @"SimpleCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleCellIdentifier"];
....
return cell
}
Then in another class, I’ll see the same snippet for stock UITableViewCells. I was wondering what happens to the
static NSString *simpleCellIdentifier;
Since it’s static, it gets allocated for the lifetime of the project right? So if the code in another viewController gets run, what happens? Does it just use the old simpleCellIdentifier that was created in the other class? Thanks.
In this case
simpleCellIdentifieronly exists within the method scope. So there can be as manysimpleCellIdentifierin different methods as you like because they are different instances.If you declare the
staticvariable within the class scope, then you are working with the same instance whenever you read/write that variable in that class.