ios UICollectionView 添加 UICollectionCell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15184968/
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
UICollectionView adding UICollectionCell
提问by Matrosov Alexander
When I try to put UICollectionCell
to UICollectionView
in Interface Builder I can't put it with unknown reasons. The cell is going to the tools bar without adding to UICollectionView
当我试图把UICollectionCell
以UICollectionView
在Interface Builder,我不能把它与原因不明。单元格将转到工具栏而不添加到UICollectionView
I am using:
我在用:
- iOS SDK 6.0
- XCode 4.5.1
- I don't use Storyboard
- iOS SDK 6.0
- Xcode 4.5.1
- 我不使用故事板
回答by LE SANG
Only UICollectionView inside StoryBoard have UICollectionViewCell inside. If use XIB, create a new XIB with CellName.xib, add CollectionViewCell to it, specify name of UICollectionView custom class. After that use registerNib.
只有 StoryBoard 中的 UICollectionView 里面有 UICollectionViewCell。如果使用XIB,则使用CellName.xib 创建一个新的XIB,将CollectionViewCell 添加到其中,指定UICollectionView 自定义类的名称。之后使用 registerNib。
Sample code: https://github.com/lequysang/TestCollectionViewWithXIB
示例代码:https: //github.com/lequysang/TestCollectionViewWithXIB
回答by Anil Varghese
You cannot put UICollectionViewCell
directly into the UiCollectionView
if you are using Xib
file. Its possible only in storyboard. Add a UICollectionViewCell
into a separate Xib file. Give your class name. Then register either class or xib before the collection view appears
如果您正在使用文件UICollectionViewCell
,UiCollectionView
则不能直接放入Xib
。它可能只在故事板中。将 a 添加UICollectionViewCell
到单独的 Xib 文件中。给出你的班级名称。然后在集合视图出现之前注册类或 xib
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:CELL_ID];
Typically this is done in viewDidLoad
.
通常这是在viewDidLoad
.
This is the implementation of a custom UICollectionViewCell
with out using Xib
这是自定义的实现,无需UICollectionViewCell
使用Xib
@implementation CollectionViewCell
@synthesize imageView = _imageView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowRadius = 5.0f;
self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
self.layer.shadowOpacity = 0.5f;
// Selected background view
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
backgroundView.layer.borderColor = [[UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:1]CGColor];
backgroundView.layer.borderWidth = 10.0f;
self.selectedBackgroundView = backgroundView;
// set content view
CGRect frame = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
self.imageView = imageView;
[imageView release];
self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
self.imageView.clipsToBounds = YES;
[self.contentView addSubview:self.imageView];
}
return self;
}
回答by RainCast
Okay. There is actually a workaround for this, if you really wanted to have the cell in collectionView inside xib file from interface builder:
好的。实际上有一个解决方法,如果您真的想在界面生成器的 xib 文件中的 collectionView 中包含单元格:
- Create a storyboard.
- Create the UICollectionView and the UICollectionViewCell from interface builder.
- Adjust the UI with constraints etc to make it look exactly what you wanted it to be.
- Create a new xib file.
- Copy everything inside the storyboard to the new xib file.
- 创建故事板。
- 从界面生成器创建 UICollectionView 和 UICollectionViewCell。
- 使用约束等调整 UI,使其看起来正是您想要的样子。
- 创建一个新的 xib 文件。
- 将故事板中的所有内容复制到新的 xib 文件中。
It will work perfectly.
它会完美地工作。
- One thing to keep in mind that step #3 is very important, because after #5 you are not supposed to drag and move around the UICollectionView, if you do, the cell will magically disappear! Other than that, it will just work perfectly.
- 要记住的一件事是第 3 步非常重要,因为在第 5 步之后你不应该在 UICollectionView 周围拖动和移动,如果你这样做,单元格会神奇地消失!除此之外,它将完美地工作。