UICollectionView 不显示单元格 [英] UICollectionView don't show the cell
问题描述
UIViewController
包含 UICollectionView
.还有 UICollectionViewCell
有自己的类 &XIB (CurrentCell.h, CurrentCell.m, CurrentCell.xib).
There is UIViewController
which contain UICollectionView
. Also there is UICollectionViewCell
which has own class & XIB (CurrentCell.h, CurrentCell.m, CurrentCell.xib).
在我的 UIViewController.h
中:
@property (strong, nonatomic) IBOutlet UICollectionView *collection;
在我的 UIViewController.m
中:
@synthesize collection;
在viewDidLoad中:
[self.collection registerClass:[CurrentCell class] forCellWithReuseIdentifier:@"cellCurRecipe"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(120, 90)];
[flowLayout setSectionInset:UIEdgeInsetsMake(5, 10, 5, 10)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.collection setCollectionViewLayout:flowLayout];
在cellForItemAtIndexPath中:
static NSString *CellIdentifier = @"cellCurRecipe";
CurrentCell *cell = (CurrentCell *)[self.collection dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
Recipes *recipe = [currRecipesArray objectAtIndex:indexPath.item];
[cell.image setImage: recipe.image];
return cell;
但是:如果我直观地点击单元格 didSelectItemAtIndexPath 方法调用.
But: if I tap intuitively to the cell didSelectItemAtIndexPath method calls.
已编辑
现在一切正常!
我忘记在 CurrentCell.m 中初始化单元格:
I forgot to init the cell in CurrentCell.m:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CurrentCell" 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;
}
推荐答案
注册笔尖,而不是类.如果单元格完全在代码中制作,您应该只需要注册一个类.如果您使用 xib,请注册(使用 registerNib:forCellWithReuseIdentifier:).如果您在故事板中制作单元格,则不要注册任何内容.
Register the nib, not the class. You should only need to register a class if the cell is made completely in code. If you use a xib, then register that (with registerNib:forCellWithReuseIdentifier:). If you make the cell in a storyboard, then don't register anything.
这篇关于UICollectionView 不显示单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!