zoukankan      html  css  js  c++  java
  • 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];

  • 相关阅读:
    删除XML文档中某节点
    水晶报表之创建子报表
    给字符串中的每个字符加上单引号
    Failed to export using the options you specified. Please check your options and try again
    从ASP.NET传递参数给水晶报表
    两个文本框异动任何一个能即时更新计算结果
    添加节点至XML文档中去
    读取XML文档存入泛型List<T>集合中
    泛型List<T>转存为XML文档
    怎样创建XML文档
  • 原文地址:https://www.cnblogs.com/hongyan1314/p/5795536.html
Copyright © 2011-2022 走看看