1) 将UICollectionView 拖入您的UIView 并调整其大小。
2) 在集合视图的.h 文件中创建一个也是IBOutlet 的属性:
@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView;
3) 再次在您的.h 文件中声明您的代表,所以现在您的.h 应该看起来像这样:
@interface UtaQuickView : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate> {
}
@property (nonatomic, retain) IBOutlet UICollectionView *myCollectionView;
4) 在故事板中连接您的 myCollectionView IBOutlet。
5)(可选)如果您的目标是 iOS6 之前的版本,请合成您的 myCollectionView 属性。如果你的目标是 iOS6,它会为你自动合成。这适用于所有属性,而不仅仅是UICollectionViews。所以在iOS6中,你根本不需要@synthesize myCollectionView = _myCollectionView。您可以在需要访问该物业的任何地方使用_mycollectionview。
6) 在您的.m 文件viewDidLoad 中,设置您的delegate 和dataSource.
_myCollectionView.delegate = self;
_myCollectionView.dataSource = self;
7) 实现所需的 dataSource 方法:
#pragma mark - UICollectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
从那里您可以根据需要实现尽可能多或尽可能少的UICollectionViewDelegate 方法。但是,根据文档需要 2 个:
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
请务必注意,您可以将<UICollectionViewDelegateFlowLayout> 替换为<UICollectionViewDelegate>,并且仍然可以访问<UICollectionViewDelegate> 中的所有方法,因为<UICollectionViewDelegateFlowLayout> 是<UICollectionViewDelegate> 的子类。
UICollectionViewDataSource Protocol Documentation
UICollectionViewDelegate Protocol Documentation