UICollectionView及其代理方法
来源:互联网 发布:实况足球经典球员数据 编辑:程序博客网 时间:2024/05/27 09:47
先上代码:
@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"集合视图"; UICollectionViewFlowLayout *collectionVIewlayout = [[UICollectionViewFlowLayout alloc]init]; //设置单元格的大小 [collectionVIewlayout setItemSize:CGSizeMake(50, 50)]; //设施最小行间距 [collectionVIewlayout setMinimumLineSpacing:20]; //设置最小列间距 [collectionVIewlayout setMinimumInteritemSpacing:20]; //设置分区间隔 [collectionVIewlayout setSectionInset:UIEdgeInsetsMake(20, 20, 20, 20)]; //集合视图的使用 UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:collectionVIewlayout]; [collectionView setBackgroundColor:[UIColor whiteColor]]; //注册Item [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"item"]; //注册头部视图 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView"]; [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footView"]; //指定代理(和表视图一样) collectionView.delegate = self; collectionView.dataSource = self; [self.view addSubview:collectionView];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - 代理方法-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 2;}-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 10;}//定义Item-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ //从重用队列中却出cell; UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath]; //获取cell的位置信息 NSLog(@"section--%ld--%ld",indexPath.item,indexPath.section); [cell setBackgroundColor:[UIColor greenColor]]; return cell;}-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.item == 0) { return CGSizeMake(80, 80); }else{ return CGSizeMake(50, 50); }}-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"我被点了");}//头尾视图-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ if ([kind isEqualToString:UICollectionElementKindSectionFooter]) { UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footView" forIndexPath:indexPath]; [footView setBackgroundColor:[UIColor blueColor]]; return footView; }else{ UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView" forIndexPath:indexPath]; [headView setBackgroundColor:[UIColor orangeColor]]; return headView; } return nil;}//定义头视图的大小-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ return CGSizeMake(100, 100);}//定义尾视图的大小-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{ return CGSizeMake(50, 30);}//边缘距离-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ }//最小行间距-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ }//最小列间距-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ }/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end记住其常用的属性和代理方法。
1 0
- UICollectionView及其代理方法
- 用UICollectionView及其controller相关UICollectionViewDataSource,UICollectionViewDelegate代理实现多列显示方法
- iOS UICollectionView基本属性 和 代理方法
- iOS 开发UICollectionView及其常用方法+实例
- iOS中UICollectionView及其常用方法
- 关于 UICollectionView 的一些代理和数据源方法
- iOS流布局UICollectionView系列二——UICollectionView的代理方法
- 本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView
- 集合视图UICollectionView 介绍及其示例程序
- UICollectionView不能实现touch方法
- 序列化及其代理
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- YII 数据缓存
- Json工具Demo(二)
- playFramework2 JDBC连接数据库
- [ubuntu]添加开机启动项
- POJ解题报告-小优
- UICollectionView及其代理方法
- Java 对 List 内对象根据对象属性进行排序
- logback--过滤器+补充
- 异常的深入研究与分析(1)
- https://projecteuler.net/problem=12
- 百思学习笔记13-推荐关注左侧
- PAT-A1046 Shortest Distance(模拟)
- eclipse设置Package Explorer的显示
- UI基础