从故事板加载集合视图单元格(而不是使用registerNib从nib加载)

时间:2013-12-10 15:32:55

标签: ios xcode storyboard xcode5 uistoryboard

在Xcode 5.0.2中,我试图为他的视频教程Learning To Build Apps For iPhone And iPad重新创建Michael Lehman的“Collection View”示例(在Safari上保护密码;第5章)。

Michael从nib文件加载一个Collection View Cell,代码如下(这里是ExploreUICollectionView/ViewController.m的完整版本):

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;

    self.cellData = [[NSMutableArray alloc]init];

    for(int i=0; i < 100; i++)
    {
        [self.cellData addObject:[NSString stringWithFormat:@"#%d", i+1]];
    }

    UINib *cellNib = [UINib nibWithNibName:@"CVCell"
                                    bundle:nil];
    [self.myCollectionView registerNib:cellNib
            forCellWithReuseIdentifier:@"CVCell"];
}

但是我不想为单元格创建单独的nib文件,而是将视图拖到Main_iphone.stroryboardMain_ipad.storyboard,如果需要,给它一个“故事板ID”然后(不知何故)在myCollectionView中使用它。

我的问题我应该打电话给哪些方法呢?

我不能在这里使用nibWithNibNameregisterNib:forCellWithReuseIdentifier

更新: rdelmar的建议has worked(谢谢!):

enter image description here

1 个答案:

答案 0 :(得分:13)

您无法将视图拖动到故事板中,只能查看控制器。要在故事板中执行此操作,请将集合视图添加到控制器,或拖出集合视图控制器。您将在该集合视图中获得一个单元格,您可以在其中添加所需的任何子视图。为单元格提供重用标识符,不要在控制器代码中注册任何内容。如果需要,可以创建UICollectionViewCell的子类,并将该单元的类更改为您的子类。