0

我正在查看一些代码,想知道它是如何工作的。在一节课上,我看到这样的事情:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleCellIdentifier = @"SimpleCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleCellIdentifier"];
....
return cell
}

然后在另一堂课中,我将看到库存 UITableViewCells 的相同片段。我想知道会发生什么

static NSString *simpleCellIdentifier;

由于它是静态的,它会在项目的整个生命周期内分配,对吗?那么如果另一个 viewController 中的代码运行起来,会发生什么?它只是使用simpleCellIdentifier在其他类中创建的旧的吗?谢谢。

4

1 回答 1

3

在这种情况下simpleCellIdentifier只存在于方法范围内。所以可以有尽可能多simpleCellIdentifier的不同方法,因为它们是不同的实例。

如果您static在类范围内声明变量,那么每当您在该类中读取/写入该变量时,您都在使用同一个实例。

于 2012-07-06T20:47:23.927 回答