collectionview使用
2016-07-23 20:34
246 查看
创建UICollectionViewFlowLayout 对象来设置相关的布局,包括itemSize,headerReferenceSize,sectionInset。设置对应的布局大小,相关的和顶部之间的间距等。
UICollectionView创建对应的view并且定义对应的大小,设置代理方法和数据源对象。
-(void)loadCollectionView { UICollectionViewFlowLayout * collectionViewFlow = [[UICollectionViewFlowLayout alloc]init]; UICollectionView *collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, ScreenHeight, CGRectGetWidth(_homeScrollView.frame), 2000)collectionViewLayout:collectionViewFlow]; collectView.dataSource = self; collectView.delegate = self; collectView.backgroundColor = [UIColor blueColor]; [collectView registerClass:[CSHomeCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"]; collectionViewFlow.itemSize = CGSizeMake(93, 120); collectionViewFlow.sectionInset = UIEdgeInsetsMake(40, 0, 0, 0); collectionViewFlow.scrollDirection = UICollectionViewScrollDirectionVertical; [_homeScrollView addSubview:collectView]; } //datasource,delegate -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 20; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CSHomeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath]; cell.topImage.image = [UIImage imageNamed:@"cludy_bg"]; return cell; }
从而来实现对应的方法,其中自定义的cell类
-(instancetype)initWithFrame:(CGRect)frame { self =[super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor grayColor]; self.topImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, MYSIZE.width-10, MYSIZE.height*0.6)]; [self addSubview:_topImage]; self.designer = [[UILabel alloc]initWithFrame:CGRectMake(5, CGRectGetHeight(self.topImage.frame), CGRectGetWidth(self.frame), 20)]; self.designer.text = @"hello"; [self addSubview:_designer]; // self.goodBtn = [UIButton alloc]initWithFrame:CGRectMake(0, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>) } return self; }
当定义的时候,自动调用initwithframe函数来初始化
相关文章推荐
- 挑战这十七项编程 锻炼大脑并且提升能力
- Nova 是如何统计 OpenStack 计算资源
- android 子线程中更新界面?被ProgressBar给迷惑了
- 2016第30周六
- POJ 2553 The Bottom of a Graph
- Mac下运行 x86 的 AVD
- Hust oj 1071 Jumping Jack(贪心)
- 大数据Spark “蘑菇云”行动前传第5课:零基础实战Scala函数式编程及Spark源码解析
- 1001数组中和等于k的数对
- webstorm笔记
- 星际之门(一)(cayley定理n阶完全图的生成数个数,快速幂)
- KMP算法 HDU 1686 Oulipo 也就是乌力波啦~~
- 图像文件读入和显示
- 什么是一个对象?
- (贪心) Vacations 求休息的最少天数
- 对象的引用
- 网络修复-可以上qq但是网页等打不开
- NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorld
- 迭代加深搜索
- 双系统ubuntu16.04 64位安装android studio并启动虚拟机