UICollectionView详解
来源:互联网 发布:数据库审计产品资料 编辑:程序博客网 时间:2024/06/10 04:49
今天,将和大家一起学习UICollectionView,UIcollectionView自出来后,一直受追捧,确实好用。今天有朋友问我如何添加heardView,我简单的回答:tableview如何添加,那么CollectionView就怎么添加,后来经过自己实验发现确实不是那回事,所以列出一些自己犯的错误,供大家参考。
1.首先实例化一个 UICollectionViewFlowLayout因为要设置item,itemSize,等一些属性。
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
- layout.itemSize = CGSizeMake(100,50); //CGSizeMake(200 , 200);
- layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
- layout.minimumLineSpacing = 5;
2.实例化一个UICollectionView
- myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.height, self.view.frame.size.width) collectionViewLayout:layout];
- myCollectionView.delegate = self;
- myCollectionView.dataSource = self;
- myCollectionView.backgroundColor = [UIColor blueColor];
- [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewIdentifier"];
- [myCollectionView registerClass:[CollectionHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
- [self.view addSubview:myCollectionView];
- #pragma mask - dataSource
- -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
- UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewIdentifier" forIndexPath:indexPath];
- for (UIView*view in cell.contentView.subviews) {
- if (view) {
- [view removeFromSuperview];
- }
- }
- UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 40, 40)];
- lab.backgroundColor = [UIColor yellowColor];
- lab.text = [NSString stringWithFormat:@"%d",[indexPath row]+[indexPath section]*3];
- cell.backgroundColor = [UIColor redColor];
- // cell.backgroundView.backgroundColor = [UIColor whiteColor];
- [cell.contentView addSubview:lab];
- UIView *view = [[UIView alloc] initWithFrame:cell.bounds];
- [view setBackgroundColor:[UIColor whiteColor]];
- cell.selectedBackgroundView = view;
- return cell;
- };
- //cell的大小
- -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
- return CGSizeMake(50, 50);
- }
- -(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
- return 10;
- };
- -(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
- return 10;
- };
- #pragma mask - delegate
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
- NSLog(@"点击%@",indexPath);
- }
- - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
- {
- return YES;
- }
4.设置heardView,hearView需要自定义,并且继承
5.自定义完成后,需要在代理方法中从缓存池中找到已经注册的heardView
- - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
- {
- CollectionHeadView *heard = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
- heard.lable.text = [NSString stringWithFormat:@"第%d组",indexPath.section];
- return heard;
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
- return CGSizeMake(320, 40);
- }
6.效果图
大家按照代码一步一步来,肯定就是超简单。
0 0
- UICollectionView详解
- UICollectionView详解
- UICollectionView详解
- UICollectionView详解
- UICollectionView详解
- UICollectionView详解
- UICollectionView详解
- UICollectionView 详解
- UICollectionView详解
- 详解UICollectionView
- UICollectionView详解
- UICollectionView详解
- UICollectionView详解
- UICollectionView详解
- UICollectionView详解
- UICollectionView详解
- UICollectionView详解
- UICollectionView详解
- jsp内置对象page对象、pageContext对象、Config对象、exception对象
- cxf webservice生成wsdl文件错误
- poj1273 EK
- git更换仓库地址
- C++ 注意事项
- UICollectionView详解
- 杭电--水仙花数
- hiho一下 第二十三周---最短路径·一:Dijkstra算法
- Codeforces Round #225 (Div. 1)C(dfs+线段树)
- Odoo文档管理/知识管理应用实践 - 上传附件
- 软件测试基础知识
- Mac OS X 10.10更新及体验
- /bin,/sbin,/usr/sbin,/usr/bin 目录之简单区别
- C# 参考之上下文关键字get、set、value、partial、where和yield