UICollectionCell

本文详细介绍了UICollectionView的应用,包括布局设置、数据源与代理方法,以及如何实现个性化cell定制与标题栏展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

=================

UICollectionView的应用

=================

用于显示方块Cell

代理:<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

 

//布局

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

    //横向间距

    flowLayout.minimumLineSpacing=30;

    //竖向间距

    flowLayout.minimumInteritemSpacing=20;

    //滚动的方向 flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;

    

 

    //创建UICollectionView

    _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, KSCREENWIDTH, KSCREENHEIGHT-64-49) collectionViewLayout:flowLayout];

    _collectionView.delegate=self;

    _collectionView.dataSource=self;

    

    _collectionView.backgroundColor=[UIColor whiteColor];

    

    [self.view addSubview:_collectionView];

 

#pragma mark collectionView的代理

//返回每组有多少个对象

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

    return _dataArray.count;

}

 

//返回cell的大小

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

    return CGSizeMake(60, 80);

}

 

//返回距离四周的边距,上,左,下,右

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

    return UIEdgeInsetsMake(30, 30, 30, 30);

}

 

//cell的定制

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

    static NSString *cellId=@"cell";

    SetCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];

    SetModel *model=_dataArray[indexPath.row];

    cell.itemName.text=model.name;

    cell.itemImage.image=[UIImage imageNamed:model.imageUrl];

    return cell;

}

 // 标题栏

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader) {

        if (indexPath.section == 0) {

            JYGP3ReusableView *header = [collectionView  dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadView" forIndexPath:indexPath];

            header.text = @"预设食谱";

            

            reusableview = header;

            

            UIButton *netCookBut = [UIButton buttonWithType:UIButtonTypeCustom];

            [netCookBut setBackgroundImage:[UIImage imageNamed:@"ic_p3_btn_add"] forState:UIControlStateNormal];

            [netCookBut setTitle:@"+网络食谱" forState:UIControlStateNormal];

            [netCookBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

            netCookBut.titleLabel.font = [UIFont systemFontOfSize:10];

            netCookBut.frame = CGRectMake(ScreenWidth - 63 - 7, 17, 63, 20);

            [netCookBut addTarget:self action:@selector(addNetCookbook) forControlEvents:UIControlEventTouchUpInside];

            [header addSubview:netCookBut];

        }

        if (indexPath.section == 1) {

            JYGP3ReusableView *header = [collectionView  dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerID" forIndexPath:indexPath];

            header.text = @"我的食谱";

            reusableview = header;

        }

    }

    return reusableview;

}

 

 

转载于:https://www.cnblogs.com/tony0571/p/5456715.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值