2012-07-06 51 views
0

我在看一些代碼,並想知道這是如何工作的。在一個類中,我看到這樣的事情:UITableViewCellIdentifier的靜態NSString

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

然後在另一個班,我會看到相同的片段股票UITableViewCells。我不知道,因爲它是靜態的,它就會被分配給項目右側的一生會發生什麼變化

static NSString *simpleCellIdentifier; 

?所以如果另一個viewController中的代碼運行,會發生什麼?它只是使用在其他課程中創建的舊simpleCellIdentifier?謝謝。

回答

3

在這種情況下,simpleCellIdentifier只存在於方法範圍內。所以可以使用不同的方法儘可能多地使用不同的方法,因爲它們是不同的實例。

如果您在類範圍內聲明static變量,那麼無論何時在該類中讀取/寫入該變量時,您都使用同一個實例。