2011-12-05 44 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,然后自动释放它。]

相关问题