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;  

    }

     
  • 相关阅读:
    Codeforces Round #461 (Div. 2)B-Magic Forest+位运算或优雅的暴力
    动态规划:树形DP
    动态规划:划分DP
    动态规划:状压DP
    图论:树的直径
    图论:点分治
    图论:2-SAT
    数据结构&图论:K短路-可持久化可并堆
    图论:次短路
    图论:曼哈顿距离最小生成树
  • 原文地址:https://www.cnblogs.com/huoxingdeguoguo/p/4630948.html
Copyright © 2011-2022 走看看