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;
        }
        
    }
  • 相关阅读:
    高德全链路压测平台TestPG的架构与实践
    性能测试之稳定性测试(可靠性测试)
    服务端高并发分布式架构演进之路
    高性能高并发系统的稳定性保障
    聊聊服务稳定性保障这些事
    qt 待研究
    k73 uboot 和emmc启动移植
    Qt更新组件出现(“要继续此操作,至少需要一个有效且已启用的储存库”)
    C++ Lambda 编译器实现原理
    qt 网络编程参考资料
  • 原文地址:https://www.cnblogs.com/limicheng/p/3893003.html
Copyright © 2011-2022 走看看