码迷,mamicode.com
首页 > 其他好文 > 详细

UICollectionView的使用方法

时间:2015-07-08 20:19:30      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

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;  

}

 

UICollectionView的使用方法

标签:

原文地址:http://www.cnblogs.com/huoxingdeguoguo/p/4630948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!