iphone - 为什么使用 UINib 将 customCell 属性设置为 nil

标签 iphone uitableview uinib

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

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

最佳答案

你必须看看这一行:

[quoteCellNib instantiateWithOwner:self options:nil];

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

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

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

关于iphone - 为什么使用 UINib 将 customCell 属性设置为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8389334/

相关文章:

iphone - 如何覆盖 UITableViewCell 中的 accessoryView 以更改其位置

ios - 在 ios 7 xcode 中单击 ScrollView 上方的按钮时如何滚动 ScrollView ?

ios - 使用 UITableViewCellAccessoryCheckMark

iphone - iOS 中的通话记录、短信记录、电子邮件记录

uitableview - 在 Swift 中显示导航栏

ios - CellForRowAtIndexPath 被意外调用

iOS内存泄漏与 Nib

ios - 从 xib 加载 UITableViewCell(此类不符合键的键值编码)

ios - 'NSInternalInconsistencyException' 原因 : 'Could not load NIB in bundle' when dequeuing tableview cell

iphone - UITextfield 显示在键盘顶部 - 初学者