xcode 从故事板加载集合视图单元格(而不是从带有 registerNib 的笔尖)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20498521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:24:59  来源:igfitidea点击:

Loading a Collection View Cell from storyboard (instead of from nib with registerNib)

iosxcodestoryboardxcode5uistoryboard

提问by Alexander Farber

In Xcode 5.0.2 I'm trying to recreate Michael Lehman's "Collection View" example for his video tutorial Learning To Build Apps For iPhone And iPad(password protected on Safari; the chapter 5).

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

Michael loads a Collection View Cell from a nib file with the following code (here the full version of his ExploreUICollectionView/ViewController.m):

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"];
}

I however would like not to create a separate nib file for the cell, but drag a View to the Main_iphone.stroryboardand Main_ipad.storyboard, give it a "Storyboard ID" if needed and then (somehow) use it in myCollectionView.

然而,我不想为单元格创建一个单独的 nib 文件,而是将一个视图拖到Main_iphone.stroryboardMain_ipad.storyboard,如果需要,给它一个“故事板ID”,然后(以某种方式)在myCollectionView.

My question iswhich methods should I call then please?

我的问题是我应该调用哪些方法?

I can not use a nibWithNibNameand registerNib:forCellWithReuseIdentifierhere.

我不能在这里使用 anibWithNibNameregisterNib:forCellWithReuseIdentifier

UPDATE:rdelmar's advice has worked(thanks!):

更新:rdelmar 的建议有效(谢谢!):

enter image description here

在此处输入图片说明

回答by rdelmar

You can't drag a view into a storyboard, only view controllers. To do it in a storyboard, you add a collection view to a controller, or drag out a collection view controller. You will get a single cell in that collection view, to which you can add any subviews you want. Give the cell a reuse identifier, and don't register anything in your controller code. You can make a subclass of UICollectionViewCell if you want, and change the class of that cell to your subclass.

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