zoukankan      html  css  js  c++  java
  • 对UICollectionView的学习

    UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类

    与UICollectionView有关的三个协议:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout

    几个常用到得方法(需遵守协议)

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //创建一个布局方式
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.headerReferenceSize = CGSizeMake(320, 50);//header的size
        layout.footerReferenceSize = CGSizeMake(320, 20);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 20, 0);
        //创建一个集合视图
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        [collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"cell"];
        //注册header
        [collectionView registerClass:[CustomHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        //注册footer 增补视图
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
        
        [self.view addSubview:collectionView];
        [layout release];
        [collectionView release];
        // Do any additional setup after loading the view.
    }
    //定义展示的Section的个数
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
    {
        return 2;
    }
    //定义展示的UICollectionViewCell的个数
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return 10;
    }
    
    //每个UICollectionViewCell展示的内容
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *identifier = @"cell";
        CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
        cell.titleLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
        cell.backgroundColor = [UIColor orangeColor];
        return cell;
    }
    //定义每个UICollectionView 中cell的大小
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        return CGSizeMake(100, 200);
    }
    //增补视图header与footer
    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
    {
        if (kind == UICollectionElementKindSectionHeader) {
            CustomHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
            view.backgroundColor = [UIColor greenColor];
            return view;
        }else
        {
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
            view.backgroundColor = [UIColor redColor];
            return view;
        }
        
    }
  • 相关阅读:
    BZOJ4944 泳池 解题报告
    简短的开始
    树链剖分的一种妙用与一类树链修改单点查询问题的时间复杂度优化——2018ACM陕西邀请赛J题
    三月月考暨省队选拔
    Luogu P1245 电话号码
    JXOJ(基于UOJ)部署日志
    入学考试总结_20190310
    十二月月考之生物总结
    寒假作业完成进度
    discuz在windows下的环境配置遇到的问题总结
  • 原文地址:https://www.cnblogs.com/limicheng/p/3893003.html
Copyright © 2011-2022 走看看