3

我正在查看一些使用 UITableView 的 cellForRowAtIndexPath 的 UINib 方法的 Apple 示例代码:

-(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;

有人可以解释最后两行发生了什么吗?谢谢。

4

1 回答 1

1

你必须看看这一行:

[quoteCellNib instantiateWithOwner:self options:nil];

也就是说,NIB 以当前对象作为所有者进行实例化。大概在您的 NIB 中,您已经正确设置了文件的所有者类,并且IBOutlet在该类中有一个名为quoteCell. 因此,当您实例化NIB 时,它将在您的实例中设置该属性,即它设置self.quoteCell为新创建的单元格。

但是您不想让属性指向该单元格,因为您只是将其用作临时变量来访问该单元格。所以你设置cellself.quoteCell这样你就可以从那个函数中返回它。然后你不再需要self.quoteCell,所以你摆脱它。

[顺便说一句,我假设这是使用ARC?否则你会想要保留cell然后自动释放它。]

于 2011-12-05T18:21:33.367 回答