iOS中自定义UICollectionView的用法

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];

 

posted @ 2016-08-22 14:37  瀚海枭龙  阅读(5647)  评论(0编辑  收藏  举报