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;  

}

 
posted on 2015-07-08 17:54  火星的蝈蝈  阅读(241)  评论(0编辑  收藏  举报