UICollectionView的基本使用

UICollectionView和UITableView有很多类似之处,可以先温习一下tableView。UICollectionView继承UIScrollView,有两个代理方法和一个UICollectionViewDelegateFlowLayout,我们目前先了解UICollectionView的基础用法。 

1.初始化的时候必须有UICollectionViewFlowLayout对象,如果没有UICollectionViewFlowLayout对象,UICollectionView是无法显示出来的。

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];


2.如果要设置一个headerView(头视图),先要设置HeaderReferenceSize即头视图的frame,然后注册头视图。

     //设置flowLayout的头视图frame

    [flowLayout setHeaderReferenceSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.width*3/4)];

    //注册头视图以便复用头视图 

  [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader   withReuseIdentifier:@"self.navView"]; 

3.注册cell。一般我们自己要写一个UICollectionCell来自定义cell的显示。

    [self.collectionView registerClass:[CollectionViewCell classforCellWithReuseIdentifier:@"cell"];


部分代码:

-(void)initCollectionView{



   UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];


    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(064screenWscreenH-64)collectionViewLayout:flowLayout];

    self.collectionView.backgroundColor = [UIColor whiteColor];

    self.collectionView.delegate = self;

    self.collectionView.dataSource = self;

    [self.collectionView registerClass:[CollectionViewCell classforCellWithReuseIdentifier:@"cell"];

    [self.view addSubview:self.collectionView];


}

//返回UICollectionView有几个section

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    

    return 1;

}

//每个section有几个Item

-(NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    return cellCityArray.count;

    

}


//返回UICollectionViewCell的代理方法,处理复用cell

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{



static NSString *CellIdentifier = @"cell";

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    

    [cell sizeToFit];

    cell.imgView.image = [UIImage imageNamed:cellImageArray[indexPath.item]];

    cell.cityText.text = cellCityArray[indexPath.item];


    return cell;

    


}

//调节每个item的大小size代理方法

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{


    return CGSizeMake(collectionWidth, 3*collectionWidth/5);

    

}

//调节每个itemedgeInsets代理方法

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{



    return UIEdgeInsetsMake(10, 20, 10, 20);

}

//点击每个item事件的代理方法

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{


    NSString *string = cellCityArray[indexPath.item];

    self.selectString(string);

    [self dismissViewControllerAnimated:YES completion:nil];

    

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以为您提供一些关于UICollectionView基本使用的代码示例,您可以参考以下代码: //1.创建UICollectionView,指定layout UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init]; UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout]; //2.注册UICollectionViewCell [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"]; //3.实现UICollectionViewDataSource协议方法 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return dataArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath]; //设置cell内容 return cell; } //4.设置UICollectionViewDelegateFlowLayout方法,指定cell size等布局属性 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ return CGSizeMake(100, 100); } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ return UIEdgeInsetsMake(10, 10, 10, 10); } //5.添加UICollectionView到视图中 [self.view addSubview:collectionView]; 以上只是示例代码,具体还需根据您的需求来实现。希望对您有帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值