2011-12-05 86 views
3

我使用的cellForRowAtIndexPath的UINib方法的UITableView看一些蘋果的示例代碼:爲什麼customCell屬性設置爲無使用UINib

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { 
     static NSString *QuoteCellIdentifier = @"QuoteCellIdentifier"; 
     QuoteCell *cell = (QuoteCell*)[tableView dequeueReusableCellWithIdentifier:QuoteCellIdentifier]; 
     if (!cell) { 
       UINib *quoteCellNib = [UINib nibWithNibName:@"QuoteCell" bundle:nil]; 
     [quoteCellNib instantiateWithOwner:self options:nil]; 
     cell = self.quoteCell; 
     self.quoteCell = nil; 

我不太明白的最後兩行

 cell = self.quoteCell; 
     self.quoteCell = nil; 

有人可以解釋最後兩行中發生了什麼嗎?謝謝。

回答

1

你必須看看這個行:

[quoteCellNib instantiateWithOwner:self options:nil]; 

那是說給筆尖與當前對象的所有者實例。大概在你的NIB中,你已經正確設置了文件的所有者類,並且在該類中有IBOutlet屬性quoteCell。因此,當您實例化NIB時,它會在您的實例中設置該屬性,即將self.quoteCell設置爲新創建的單元格。

但是,您不希望將該屬性指向該單元格,因爲您剛剛將它用作臨時變量來訪問該單元格。因此,您將cell設置爲self.quoteCell,以便您可以從該函數返回它。那麼你不再需要self.quoteCell,所以你擺脫它。

[順便說一下,我認爲這是使用ARC?否則,您會希望保留cell,然後自動釋放它。]

相關問題