1.實現自定義CollectionView首先繼承CollectionView.
舉例:
ZLHotAdvisorCollectionView.h文件
@interface ZLHotAdvisorCollectionView : UICollectionView
ZLHotAdvisorCollectionView.m文件
#import "ZLHotAdvisorCollectionView.h"
static NSString *ID = @"ZLHotAdvisorCollectionViewCell";注:ZLHotAdvisorCollectionViewCell這里是自定義的UICollectionViewCell
@interface ZLHotAdvisorCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@end
@implementation ZLHotAdvisorCollectionView
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
self = [super initWithFrame:frame collectionViewLayout:layout];
if (self) {
self.dataSource = self;
self.delegate = self;
[self registerClass:[ZLHotAdvisorCollectionViewCell class] forCellWithReuseIdentifier:ID];
//如果collectionView有頭的話,那么寫上它,注冊collectionview的頭部視圖,
[self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZLServiceHomePageCollectionViewheaderOne"];
//如果collectionView有頭的話,那么寫上它,注冊collectionview的尾部視圖,
[self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ZLServiceHomePageCollectionViewFooterThree"];
self.backgroundColor = IWTextColorBeiJi;
}
return self;
}
下面是代理方法
//返回collection view里區(section)的個數,如果沒有實現該方法,將默認返回1:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 3;
}
//返回指定區(section)包含的數據源條目數(number of items),該方法必須實現:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 4;
}
//返回某個indexPath對應的cell,該方法必須實現:
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ZLAllServicesNewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
return cell;
}
//設定collectionView(指定區)的邊距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(6, 6, 6,6);
}
//點擊每個item實現的方法:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//設置footer和header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {//這是頭部視圖
UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZLServiceHomePageCollectionViewheaderOne" forIndexPath:indexPath];
UIView *SectionHeadView = [[UIView alloc]init];
SectionHeadView.backgroundColor = IWTextColorBeiJi;
SectionHeadView.frame =CGRectMake(0, 0, 320, 200);注意:這里設置的寬高必須和返回頭部視圖設置的寬高一樣
[header addSubview:SectionHeadView];
return header;
} else if([kind isEqualToString:UICollectionElementKindSectionFooter]){//尾部視圖
UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ZLServiceHomePageCollectionViewFooterOne" forIndexPath:indexPath];
UIView *SectionFooterView = [[UIView alloc]init];
SectionFooterView.backgroundColor = IWTextColorBeiJi;
SectionFooterView.frame =CGRectMake(0, 0, 320, 200);注意:這里設置的寬高必須和返回尾部視圖設置的寬高一樣
[header addSubview:SectionFooterView];
return footer;
}else{
return nil;
}
}
在使用頭部或者尾部視圖的時候,這兩個方法必須實現,否則出不來。
//設置footer尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//設置header尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
在用的時候
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
CGFloat itemSizew = (SCREEN_WIDTH - (2 + 1.0) * 6) / 2;
layout.itemSize = CGSizeMake(itemSizew, (itemSizew * 3) / 4 + 45 + 40);
layout.minimumInteritemSpacing = 6;
//這里換成自定義的collectionView
self.ZLHotAdvisorCollectionView = [[ZLHotAdvisorCollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 50) collectionViewLayout:layout];