iOS-UICollectionViewController协议及回调
Posted Vijay的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS-UICollectionViewController协议及回调相关的知识,希望对你有一定的参考价值。
一.UICollectionViewDataSource
1.返回Section数量的方法
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView { return 5; }
2.返回每个Section中Cell的数量的方法
- (NSInteger)collectionView: (UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section { return 30; }
3.选择CollectionView中所使用的Cell
- (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView cellForItemAtIndexPath: (NSIndexPath *)indexPath { //通过Cell重用标示符来获取Cell CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: reuseIdentifier forIndexPath: indexPath]; return cell; }
4.注册UICollectionReusableView的方法。
UINib *headerNib = [UINib nibWithNibName: @"CollectionHeaderReusableView" bundle: [NSBundle mainBundle]]; //注册重用View [self.collectionView registerNib: headerNib forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"CollectionHeaderReusableView"]; //注册FooterView UINib *footerNib = [UINib nibWithNibName: @"CollectionFooterReusableView" bundle:[ NSBundle mainBundle]]; [self.collectionView registerNib: footerNib forSupplementaryViewOfKind: UICollectionElementKindSectionFooter withReuseIdentifier: @"CollectionFooterReusableView"];
5.在UICollectionViewDataSource中的设置Supplementary View
- (UICollectionReusableView *)collectionView: (UICollectionView *)collectionView viewForSupplementaryElementOfKind: (NSString *)kind atIndexPath: (NSIndexPath *)indexPath{ //设置SectionHeader if ([kind isEqualToString: UICollectionElementKindSectionHeader]) { UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionHeaderReusableView" forIndexPath:indexPath]; return view; } //设置SectionFooter UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionFooterReusableView" forIndexPath:indexPath]; return view; }
二.UICollectionViewDelegateFlowLayout
1.Cell定制尺寸
- (CGSize)collectionView: (UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *)indexPath{ if (indexPath.section == 0) { return CGSizeMake(50, 50); } return CGSizeMake(60, 60); }
2.改变Section的上下左右边距--UIEdgeInsetsMake
- (UIEdgeInsets)collectionView: (UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex: (NSInteger)section{ if (section == 0) { return UIEdgeInsetsMake(50, 50, 50, 50); } return UIEdgeInsetsMake(0, 0, 0, 0); }
3.每个Cell的上下边距
- (CGFloat)collectionView: (UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex: (NSInteger)section{ if (section == 0) { return 5.0f; } return 20.0f; }
4.设置Cell的左右边距
- (CGFloat)collectionView: (UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex: (NSInteger)section{ if (section == 0) { return 5.0f; } return 20.0f; }
5.设置Header View和Footer View的大小
- (CGSize)collectionView: (UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection: (NSInteger)section{ return CGSizeMake(200, 50); } - (CGSize)collectionView: (UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection: (NSInteger)section{ return CGSizeMake(200, 50); }
三.UICollectionViewDelegate
1.设置Cell可以高亮
- (BOOL)collectionView: (UICollectionView *)collectionView shouldHighlightItemAtIndexPath: (NSIndexPath *)indexPath{ return YES; }
2.Cell从非高亮变为高亮状态和从高亮变为非高亮状态时回调用下面的方法
- (void)collectionView: (UICollectionView *)collectionView didHighlightItemAtIndexPath: (NSIndexPath *)indexPath{ [self changeHighlightCellWithIndexPath:indexPath]; } - (void)collectionView: (UICollectionView *)collectionView didUnhighlightItemAtIndexPath: (NSIndexPath *)indexPath{ [self changeHighlightCellWithIndexPath:indexPath]; }
3.设定Cell是否可选
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{ return YES; }
4.Cell支持多选
self.collectionView.allowsMultipleSelection = YES;
5.在多选状态下需要支持取消Cell的多选
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath{ return YES; }
6.Cell将要出现,Cell出现后,Supplementary View将要出现以及Supplementary View已经出现
/** * Cell将要出现的时候调用该方法 */ - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_ios(8_0){ NSLog(@"第%ld个Section上第%ld个Cell将要出现",indexPath.section ,indexPath.row); } /** * Cell出现后调用该方法 */ - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"第%ld个Section上第%ld个Cell已经出现",indexPath.section ,indexPath.row); } /** * headerView或者footerView将要出现的时候调用该方法 */ - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0){ NSLog(@"第%ld个Section上第%ld个扩展View将要出现",indexPath.section ,indexPath.row); } /** * headerView或者footerView出现后调用该方法 */ - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{ NSLog(@"第%ld个Section上第%ld个扩展View已经出现",indexPath.section ,indexPath.row); }
以上是关于iOS-UICollectionViewController协议及回调的主要内容,如果未能解决你的问题,请参考以下文章