欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

objective-C CollectionView 加深(添加注册头部View)

发布时间:2025/3/8 编程问答 39 豆豆
生活随笔 收集整理的这篇文章主要介绍了 objective-C CollectionView 加深(添加注册头部View) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘Python工程师标准>>>

#pragma marc 添加Collection -(void)addConllectionView{//collection的布局方案UICollectionViewFlowLayout *collectionViewLayout=[[UICollectionViewFlowLayout alloc]init];//设置位置大小以及布局方案_myCollectionView=[[UICollectionView alloc]initWithFrame:CM(0, 0, VIEW_WIDTH,VIEW_HEIGHT-64-48) collectionViewLayout:collectionViewLayout];//CollectionView的背景颜色_myCollectionView.backgroundColor=[UIColor whiteColor];[self addSubview:_myCollectionView];//设置代理_myCollectionView.delegate=self;_myCollectionView.dataSource=self;//注册Cell 以及选择控制的类[_myCollectionView registerClass:[ExhibitionCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];//注册Head以及Food 以及选择控制的类[_myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Head"];} #pragma mark 设置Cell -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ExhibitionCollectionViewCell *cell=[_myCollectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];cell.backgroundColor=[UIColor whiteColor];cell.imageView.image=[UIImage imageNamed:@"商品"];return cell; } #pragma mark 设置头部以及尾部 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {//和判断复用一样UICollectionReusableView * head = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Head" forIndexPath:indexPath];//添加轮播图[head addSubview:_carouselFigureView];return head;}return nil;} #pragma mark 设置Cell返回数量 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{return 20; } #pragma mark 定义展示的组数 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {return 1; }#pragma mark 设置cell的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {return CGSizeMake(VIEW_WIDTH/3.55f, VIEW_HEIGHT/3.550f);}#pragma mark 设置每个section中不同的行之间的行间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {return 20; } #pragma mark 设置每个section中不同的列之间的间距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {return 10; } #pragma mark 设置每个Cell的内边距 -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {return UIEdgeInsetsMake(0, 0, 10, 0);//分别为上、左、下、右 } #pragma mark 点击事件 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{CommodityDisplayControllerView * CDVC=[[CommodityDisplayControllerView alloc]init];[[self getController] presentViewController:CDVC animated:YES completion:nil];NSLog(@"%ld",indexPath.row); }#pragma mark 返回头headerView的大小 -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{CGSize size={VIEW_WIDTH,IMG_EXHIBITION_HEIGHT};return size; }


转载于:https://my.oschina.net/u/2483781/blog/645035

总结

以上是生活随笔为你收集整理的objective-C CollectionView 加深(添加注册头部View)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。