我正在努力学习如何使用UICollectionView.该文件是一个有点难以理解,而我发现教程无论是在目标C或长期复杂的项目当中.
当我学习如何使用时UITableView,我们❤Swift的 如何用iOS 8和Swift制作一个简单的桌面视图有一个非常基本的设置和解释让我去.有这样的东西UICollectionView吗?
下面的答案是我试图学习如何做到这一点.
有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 …Run Code Online (Sandbox Code Playgroud)