【问题标题】:Why set customCell property to nil using UINib为什么使用 UINib 将 customCell 属性设置为 nil
【发布时间】:2011-12-05 17:29:16
【问题描述】:

我正在查看一些使用 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;

有人能解释一下最后两行发生了什么吗?谢谢。

【问题讨论】:

    标签: iphone uitableview uinib


    【解决方案1】:

    你必须看看这一行:

    [quoteCellNib instantiateWithOwner:self options:nil];
    

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多