我正在看一些使用cellForRowAtIndexPath的UITableView的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;有人能解释一下最后两行发生了什么吗?谢谢。
发布于 2011-12-06 02:21:33
你必须看这一行:
[quoteCellNib instantiateWithOwner:self options:nil];这就是告诉NIB使用当前对象作为所有者进行实例化。大概在您的NIB中,您已经正确地设置了文件的所有者类,并且在该类中有一个名为quoteCell的IBOutlet属性。因此,当您实例化NIB时,它会在实例中设置该属性,即将self.quoteCell设置为新创建的单元格。
但是您不希望让属性指向该单元格,因为您只是将其用作临时变量来访问该单元格。因此,您将cell设置为self.quoteCell,这样您就可以从该函数返回它。那么你就不再需要self.quoteCell了,所以你就把它去掉吧。
顺便说一下,我假设这是使用ARC?否则,您将希望保留cell,然后自动释放它。
https://stackoverflow.com/questions/8389334
复制相似问题