[英]UICollectionView don't show the cell
有UIViewController
包含UICollectionView
。 還有UICollectionViewCell
,它具有自己的類和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方法調用。
已編輯
現在一切正常!
我忘了初始化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 :)。 如果將單元格放在情節提要中,則不要注冊任何內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.