首页 > 代码库 > iOS-UICollectionViewController协议及回调
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*)collectionViewLayoutminimumLineSpacingForSectionAtIndex: (NSInteger)section{ if (section == 0) { return 5.0f; } return 20.0f;}
4.设置Cell的左右边距
- (CGFloat)collectionView: (UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayoutminimumInteritemSpacingForSectionAtIndex: (NSInteger)section{ if (section == 0) { return 5.0f; } return 20.0f;}
5.设置Header View和Footer View的大小
- (CGSize)collectionView: (UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayoutreferenceSizeForHeaderInSection: (NSInteger)section{ return CGSizeMake(200, 50);}- (CGSize)collectionView: (UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayoutreferenceSizeForFooterInSection: (NSInteger)section{ return CGSizeMake(200, 50);}
三.UICollectionViewDelegate
1.设置Cell可以高亮
- (BOOL)collectionView: (UICollectionView *)collectionViewshouldHighlightItemAtIndexPath: (NSIndexPath *)indexPath{ return YES; }
2.Cell从非高亮变为高亮状态和从高亮变为非高亮状态时回调用下面的方法
- (void)collectionView: (UICollectionView *)collectionViewdidHighlightItemAtIndexPath: (NSIndexPath *)indexPath{ [self changeHighlightCellWithIndexPath:indexPath];}- (void)collectionView: (UICollectionView *)collectionViewdidUnhighlightItemAtIndexPath: (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协议及回调
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。