xcode UICollectionView 在 IB 中不包含 UICollectionViewCell

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12790902/
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 01:44:39  来源:igfitidea点击:

UICollectionView doesn't contain UICollectionViewCell in IB

xcodeinterface-builderios6uicollectionview

提问by Alpinista

I'm trying to add a UICollectionView to a .nib file in IB. I've worked through a few tutorials and had no problems.

我正在尝试将 UICollectionView 添加到 IB 中的 .nib 文件。我已经完成了一些教程并且没有问题。

In my existing app, when I drag a collection view into a view in IB and then expand the object, the CollectionView Flow Layout is there , but there is no collection view cell. I also cannot drag and drop a cell into the collection view. Also the collection view is displayed differently, showing an image of a grid of cells instead of the normal white box.

在我现有的应用程序中,当我将一个集合视图拖动到 IB 中的一个视图中,然后展开该对象时,CollectionView Flow Layout 在那里,但没有集合视图单元格。我也无法将单元格拖放到集合视图中。此外,集合视图的显示方式也不同,显示的是单元格网格的图像,而不是普通的白框。

I've tried creating a new view controller with nib, and have the same results when adding a collectionView.

我试过用 nib 创建一个新的视图控制器,并在添加 collectionView 时得到相同的结果。

This app WAS targeting iOS 4.2, but I've changed the deployment target to iOS 6.

此应用程序面向 iOS 4.2,但我已将部署目标更改为 iOS 6。

In any newly created projects I don't get this behavior.

在任何新创建的项目中,我都不会出现这种行为。

回答by DELUXEnized

I can't tell you why it does not work, but for me the subclass approach from this blog post solved the problem:

我不能告诉你为什么它不起作用,但对我来说,这篇博文中的子类方法解决了这个问题:

http://www.adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial.php

http://www.adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial.php

here is a short summary:

这是一个简短的总结:

  • create a new class MyViewCell which extends UICollectionViewCell and create properties, actions, outlets and methods as needed.
  • 创建一个新类 MyViewCell ,它扩展 UICollectionViewCell 并根据需要创建属性、操作、出口和方法。

@interface MyViewCell : UICollectionViewCell

@interface MyViewCell : UICollectionViewCell

  • create a View (xib file) with a Collection View Cell as its root object (and delete the object which is created by default).
  • in the attributes set the custom class of this Collection View Cell to your extended class MyViewCell (instead of the default UICollectionViewCell).
  • in the attributes under Collection Reusable View define an Identifier, e.g. myCell, you will need this later to register your call.
  • go back to your custom class and modify the inithWithFrame method to load and use your created view.

    - (id)initWithFrame:(CGRect)frame 
    { 
       self = [super initWithFrame:frame]; 
       if (self) 
       { 
         // Initialization code 
         NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CVCell" owner:self options:nil];
    
         if ([arrayOfViews count] < 1) { return nil; }
    
         if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) { return nil; }
    
         self = [arrayOfViews objectAtIndex:0];
       }
       return self;
    }
    
  • then you can register this class to be used by the collectionView with

  • 创建一个以集合视图单元格为根对象的视图(xib 文件)(并删除默认创建的对象)。
  • 在属性中将此集合视图单元格的自定义类设置为扩展类 MyViewCell(而不是默认的 UICollectionViewCell)。
  • 在集合可重用视图下的属性中定义一个标识符,例如 myCell,稍后您将需要它来注册您的呼叫。
  • 返回到您的自定义类并修改 inithWithFrame 方法以加载和使用您创建的视图。

    - (id)initWithFrame:(CGRect)frame 
    { 
       self = [super initWithFrame:frame]; 
       if (self) 
       { 
         // Initialization code 
         NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CVCell" owner:self options:nil];
    
         if ([arrayOfViews count] < 1) { return nil; }
    
         if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) { return nil; }
    
         self = [arrayOfViews objectAtIndex:0];
       }
       return self;
    }
    
  • 然后你可以注册这个类以供 collectionView 使用

[self.collectionView registerClass:[MyViewCell class] forCellWithReuseIdentifier:@"myCell"];

[self.collectionView registerClass:[MyViewCell class] forCellWithReuseIdentifier:@"myCell"];

回答by Performat

It's been a while since the question has been asked but I faced the same issue lately.

自从提出这个问题已经有一段时间了,但我最近遇到了同样的问题。

Eventually, I found the answer here.

最终,我在这里找到了答案。

In short:

简而言之:

  • You cannot drag and drop a UICollectionViewCell into a UICollectionView if you are in a .xib file. This is only possible in a storyboard.
  • The workaround is to create a .xib file for your custom cell and to register the cell manually using:

    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];

  • 如果您在 .xib 文件中,则不能将 UICollectionViewCell 拖放到 UICollectionView 中。这仅在故事板中是可能的。
  • 解决方法是为您的自定义单元创建一个 .xib 文件并使用以下方法手动注册单元:

    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];

回答by hpique

Define your cell view in a separate nib file. The view must be of type UICollectionViewCellor a subclass.

在单独的 nib 文件中定义单元格视图。视图必须是类型UICollectionViewCell或子类。

Then, you must register the nib with your collection view:

然后,您必须在集合视图中注册笔尖:

- (void)viewDidLoad
{
    UINib *cellNib = [UINib nibWithNibName:@"MyNib" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cell"];
}

回答by Zack Morris

While these answers are correct, the underlying cause of the problem is quite subtle and Apple needs to update their error message in the log. When you see:

虽然这些答案是正确的,但问题的根本原因非常微妙,Apple 需要更新日志中的错误消息。当你看到:

Unknown class MyViewCell in Interface Builder file.

What it actually means is that your source code is missing the @implementationdeclaration or that the .m file has not been added to the target. So check your code and make sure you have:

它的实际含义是您的源代码缺少@implementation声明或未将 .m 文件添加到目标中。所以检查你的代码并确保你有:

@implementation MyViewCell

@end

This is true for both UITableViewCell and UICollectionViewCell.

UITableViewCell 和 UICollectionViewCell 都是如此。

I discuss the problem further at https://stackoverflow.com/a/12755891/539149for the general case.

对于一般情况,我在https://stackoverflow.com/a/12755891/539149 上进一步讨论了这个问题。

The interesting thing is that if you are missing the @implementationand call:

有趣的是,如果您缺少@implementation并调用:

[self.myCollectionView registerClass:[MyViewCell class] forCellWithReuseIdentifier:@"MyViewCell"];

You will see:

你会看见:

Undefined symbols for architecture armv7:
  "_OBJC_CLASS_$_MyViewCell", referenced from:
      objc-class-ref in MyViewController.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

So Apple knows about the problem, they just aren't detecting it during compilation. I would advise against putting custom cells in separate nibs or calling registerClass: forCellWithReuseIdentifier:. It's much cleaner to store the custom cell inside of its container UICollectionView or UITableView in Interface Builder.

所以 Apple 知道这个问题,他们只是在编译过程中没有检测到它。我建议不要将自定义单元格放在单独的笔尖中或调用 registerClass: forCellWithReuseIdentifier:。将自定义单元格存储在其容器 UICollectionView 或 UITableView 中的界面生成器中要干净得多。