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;
        }
        
    }
  • 相关阅读:
    小米2系列板砖自救行动
    大公司都有哪些开源项目~~~阿里,百度,腾讯,360,新浪,网易,小米等
    SQLServer 2016安装时的错误:Polybase要求安装Oracle JRE 7更新51或更高版本
    异步方法不能使用ref和out的解决方法
    大公司都有哪些开源项目~~~简化版
    08.LoT.UI 前后台通用框架分解系列之——多样的Tag选择器
    07.LoT.UI 前后台通用框架分解系列之——强大的文本编辑器
    BIOS中未启用虚拟化支持系列~~例如:因此无法安装Hyper-V
    【开源】1句代码搞定图片批量上传,无需什么代码功底【无语言界限】
    06.LoT.UI 前后台通用框架分解系列之——浮夸的图片上传
  • 原文地址:https://www.cnblogs.com/limicheng/p/3893003.html
Copyright © 2011-2022 走看看