Rom*_*ski 0 ios uicollectionview uicollectionviewcell
有UIViewController哪些包含UICollectionView.还有UICollectionViewCell自己的类和XIB(CurrentCell.h,CurrentCell.m,CurrentCell.xib).
在我的UIViewController.h:
@property (strong, nonatomic) IBOutlet UICollectionView *collection;
Run Code Online (Sandbox Code Playgroud)
在我的UIViewController.m:
@synthesize collection;
Run Code Online (Sandbox Code Playgroud)
在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];
Run Code Online (Sandbox Code Playgroud)
在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;
Run Code Online (Sandbox Code Playgroud)
但是:如果我直观地点击了单元格didSelectItemAtIndexPath方法调用.
EDITED
现在一切正常!
我忘了在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;
}
Run Code Online (Sandbox Code Playgroud)
注册笔尖,而不是课程.如果单元格完全由代码构成,则只需要注册一个类.如果你使用xib,那么注册(使用registerNib:forCellWithReuseIdentifier :).如果您在故事板中创建单元格,则不要注册任何内容.
| 归档时间: |
|
| 查看次数: |
843 次 |
| 最近记录: |