我正在查看一些使用 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 中,您已正确设置文件的所有者类,并且该类中有一个名为 quoteCell
的 IBOutlet
属性。因此,当您实例化 NIB 时,它将在您的实例中设置该属性,即将 self.quoteCell
设置为新创建的单元格。
但是您不想保留指向该单元格的属性,因为您只是将其用作临时变量来访问该单元格。因此,您将 cell
设置为 self.quoteCell
,以便您可以从该函数返回它。那么你就不再需要 self.quoteCell
了,所以你可以摆脱它。
[顺便说一句,我假设这是使用 ARC?否则你会想要保留 cell
然后自动释放它。]
关于iphone - 为什么使用 UINib 将 customCell 属性设置为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8389334/