UICollectionView
2015-12-06 11:34
489 查看
#import "ViewController.h"
@interfaceViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc] init];
UICollectionView *collection = [[UICollectionViewalloc] initWithFrame:self.view.boundscollectionViewLayout:layout];//初始化,并设置布局方式
collection.backgroundColor = [UIColor whiteColor];
[collection registerClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:@"cell"];//注册UICollectionViewCell,这是固定格式,也是必须要实现的
[collection registerClass:[UICollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"headerView"];//注册头/尾视图,视图类型必须为UICollectionReusableView或者其子类,kind设置为UICollectionElementKindSectionHeader或者UICollectionElementKindSectionFooter,最后设置标识
collection.delegate = self;
collection.dataSource = self;
[self.view addSubview:collection];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//设置组数,不写该方法默认是一组
return 4;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 9;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
staticNSString *identifier = @"cell";//注意,此处的identifier要与注册cell时使用的标识保持一致
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor grayColor];
cell.layer.borderWidth = 0.5;
cell.layer.borderColor = [UIColorwhiteColor].CGColor;
return cell;
}
//设置头视图的尺寸,如果想要使用头视图,则必须实现该方法
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(300, 30);
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//根据类型以及标识获取注册过的头视图,注意重用机制导致的bug
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"headerView"forIndexPath:indexPath];
headerView.backgroundColor = [UIColor brownColor];
for (UIView *view in headerView.subviews) {
[view removeFromSuperview];
}
UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
label.text = [NSString stringWithFormat:@"%zi组的头视图",indexPath.section];
[headerView addSubview:label];
label.textColor = [UIColor whiteColor];
return headerView;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%zi组,%zi行",indexPath.section,indexPath.item);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//设置item尺寸
returnCGSizeMake(self.view.frame.size.width/3.0, 100);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//设置组距离上向左右的间距
returnUIEdgeInsetsMake(0,0,0,0);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//两个item的列间距
return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//如果一组中有多行item,设置行间距
return 0;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@interfaceViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayoutalloc] init];
UICollectionView *collection = [[UICollectionViewalloc] initWithFrame:self.view.boundscollectionViewLayout:layout];//初始化,并设置布局方式
collection.backgroundColor = [UIColor whiteColor];
[collection registerClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:@"cell"];//注册UICollectionViewCell,这是固定格式,也是必须要实现的
[collection registerClass:[UICollectionReusableViewclass] forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"headerView"];//注册头/尾视图,视图类型必须为UICollectionReusableView或者其子类,kind设置为UICollectionElementKindSectionHeader或者UICollectionElementKindSectionFooter,最后设置标识
collection.delegate = self;
collection.dataSource = self;
[self.view addSubview:collection];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//设置组数,不写该方法默认是一组
return 4;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 9;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
staticNSString *identifier = @"cell";//注意,此处的identifier要与注册cell时使用的标识保持一致
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor grayColor];
cell.layer.borderWidth = 0.5;
cell.layer.borderColor = [UIColorwhiteColor].CGColor;
return cell;
}
//设置头视图的尺寸,如果想要使用头视图,则必须实现该方法
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
return CGSizeMake(300, 30);
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//根据类型以及标识获取注册过的头视图,注意重用机制导致的bug
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"headerView"forIndexPath:indexPath];
headerView.backgroundColor = [UIColor brownColor];
for (UIView *view in headerView.subviews) {
[view removeFromSuperview];
}
UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
label.text = [NSString stringWithFormat:@"%zi组的头视图",indexPath.section];
[headerView addSubview:label];
label.textColor = [UIColor whiteColor];
return headerView;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%zi组,%zi行",indexPath.section,indexPath.item);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//设置item尺寸
returnCGSizeMake(self.view.frame.size.width/3.0, 100);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//设置组距离上向左右的间距
returnUIEdgeInsetsMake(0,0,0,0);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
//两个item的列间距
return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//如果一组中有多行item,设置行间距
return 0;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- 1062. Talent and Virtue (25)
- iOS UITableView中异步加载图片
- 1051. Pop Sequence (25)
- Android触摸事件处理机制之requestDisallowInterceptTouchEvent
- 1041. Be Unique (20)
- Android UI:机智的远程动态更新策略
- 1017. Queueing at Bank (25)
- 1007. Maximum Subsequence Sum (25)
- EasyUI中文帮助文档
- Avro技术应用_7. 将 Avro 数据读写到一个 Parquet 文件中 -- 待完善
- Hadoop中数据序列化的常用方式:SequenceFile, Avro, Thrift, ProtoBuff -- 待完善
- Ubiquitous Religions
- mvc项目架构搭建之UI层的搭建
- aJax提交——服务端不能用request存储数据,session存数据客户端可以接收到
- LibGDX_1.6: 纹理(图片)的绘制 与 assets 资源文件夹: UI 初体验
- UESTC 1253 阿里巴巴和n个大盗 博弈、策略
- server core & GUI
- UESTC 1255 斓少摘苹果 贪心法
- 谈谈UICollectionView添加头视图的方法
- 1041. Be Unique (20)【水题】——PAT (Advanced Level) Practise