zoukankan      html  css  js  c++  java
  • UICollectionView的使用方法

    1、遵守协议

    <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

    2、创建

     UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumInteritemSpacing = 10;  //最小item之间的间距
        layout.minimumLineSpacing = 10;//最小行间距
        collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64-49) collectionViewLayout:layout];
        collectionView.delegate = self;
        collectionView.dataSource = self;

    //重要

    1>、如果是用代码自定义的cell要用下面的方法注册

     [collectionView registerClass:[PicCollectionViewCell class] forCellWithReuseIdentifier:@"cc"];

    2>、如果是用xib定义的cell要用

    [collectionView registerNib:[UINib nibWithNibName:@"PicCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cc"];

    3、返回item的个数

    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return dataSourse.count;
    }

    4、cell复用

    xib和代码都用下面方法

    PicCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cc" forIndexPath:indexPath];

    5、重要协议方法

    1>返回item的大小,系统自动根据item的大小来设定每行显示的item个数(可以用layout.size方法)

    -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        CGSize  size = CGSizeMake(90, 80);
        return size;
    }

    2>//返回这个UICollectionView是否可以被选择 

    -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath  

    return YES;  

    }

     
  • 相关阅读:
    Android平板电脑开发实战详解和典型案例
    UG NX10.0技术大全(不附光盘)
    SolidWorks 2018中文版机械设计应用大全
    1192.回文字符串
    1193.矩阵转置
    1195.最长&最短文本
    1194.八进制
    1196.成绩排序
    1197.奇偶检验
    1199.找位置
  • 原文地址:https://www.cnblogs.com/huoxingdeguoguo/p/4630948.html
Copyright © 2011-2022 走看看