UICollectionView 在 IB 中不包含 UICollectionViewCell [英] UICollectionView doesn't contain UICollectionViewCell in IB
问题描述
我正在尝试将 UICollectionView 添加到 IB 中的 .nib 文件中.我已经完成了一些教程并且没有遇到任何问题.
I'm trying to add a UICollectionView to a .nib file in IB. I've worked through a few tutorials and had no problems.
在我现有的应用程序中,当我将集合视图拖到 IB 中的视图中然后展开对象时,CollectionView Flow Layout 在那里,但没有集合视图单元格.我也无法将单元格拖放到集合视图中.此外,集合视图的显示方式也有所不同,显示的是单元格网格的图像,而不是普通的白框.
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.
我尝试使用 nib 创建一个新的视图控制器,并且在添加 collectionView 时得到相同的结果.
I've tried creating a new view controller with nib, and have the same results when adding a collectionView.
此应用面向 iOS 4.2,但我已将部署目标更改为 iOS 6.
This app WAS targeting iOS 4.2, but I've changed the deployment target to iOS 6.
在任何新创建的项目中,我都没有这种行为.
In any newly created projects I don't get this behavior.
推荐答案
我不能告诉你为什么它不起作用,但对我来说,这篇博文中的子类方法解决了这个问题:
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
这里有一个简短的总结:
here is a short summary:
- 创建一个扩展 UICollectionViewCell 的新类 MyViewCell 并根据需要创建属性、操作、出口和方法.
@interface MyViewCell : UICollectionViewCell
- 创建一个以 Collection View Cell 作为根对象的 View(xib 文件)(并删除默认创建的对象).
- 在属性中将此 Collection View Cell 的自定义类设置为您的扩展类 MyViewCell(而不是默认的 UICollectionViewCell).
- 在 Collection Reusable View 下的属性中定义一个标识符,例如myCell,您稍后将需要它来注册您的通话.
返回您的自定义类并修改 inithWithFrame 方法以加载和使用您创建的视图.
- 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;
}
然后你可以注册这个类供collectionView使用
then you can register this class to be used by the collectionView with
[self.collectionView registerClass:[MyViewCell class] forCellWithReuseIdentifier:@"myCell"];
这篇关于UICollectionView 在 IB 中不包含 UICollectionViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!