ios - nib 项目中的 CellForRowAtIndexPath

标签 ios objective-c uitableview nib

我已经在我的项目中实现了 CellForRowAtIndexPath 方法。

看起来像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *cellIdentifier = @"StackTableViewCell";

    Target *target = [self.fetchedResultController objectAtIndexPath:indexPath];

    StackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"StackTableViewCell" owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }

    cell.cellLabel.text = target.body;

    cell.cellLabel.font = [UIFont fontWithName:@"Candara-Bold" size:20];[UIFont fontWithName:@"Candara-Bold" size:20];

    // Configure the cell...

    return cell;
}

但是有件事让我很困扰,那就是我真的不明白这部分发生了什么(我从一些教程中得到的):

if (!cell)
        {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"StackTableViewCell" owner:self options:nil];
            cell = [topLevelObjects objectAtIndex:0];
        }

有人可以帮我理解这个吗?

谢谢!

最佳答案

如果 TableView 没有可重复使用的单元格,请加载 StackTableViewCell.xib 中存在的对象。假设您找到的第一个定义在顶层的单元可以实例化为您想要的单元格类型。

关于ios - nib 项目中的 CellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27534768/

相关文章:

ios - 使用 initWithRelationship :attributes: 创建 RKConnectionDescription 时出现异常

objective-c - 如果我有一个使用 Obj-C block 的 for 循环,我如何识别最后一次迭代?

ios - 如何在 ios swift 的 tableview 中为 uilabel 添加滑动手势?

ios - 将 NSAttributedString 分配给 UILabel 很慢?

ios - UIImageView 缩放/淡入淡出动画?

ios - 覆盖 UIImage(名为 : )

ios - Objective-c Storyboard - UITableViewController 中的导航栏未出现

ios - Splunk 薄荷 : Failed to archive dSYMs for "MyApp" to "/tmp/splunk-mint-dsyms"

objective-c - cocoa [[NSWorkspace 共享工作空间] iconForFile :filePath caused memory leaks

ios - 带有 SpriteKit 的 UITableView