有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;
}