UICollectionView创建 xib

本文详细介绍了如何使用XIB创建UICollectionView,并通过代理与布局实现细胞与头部视图的注册与配置。

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

1.首先xib创建UICollectionView,设置代理

2.xib 创建以UICollectionViewCell为基类的类SortView3Cell,作为cell

3.xib 创建以UICollectionReusableView为基类的类SoryHeaderView,作为header

4.代码如下

#import "SortView3Cell.h"

#import "SoryHeaderView.h"

#define WIDTH ([UIScreen mainScreen].bounds.size.width)


__weak IBOutlet UICollectionView  *_collectionView;

- (void)viewDidLoad

{   //注册cell

    [_collectionView registerNib:[UINib nibWithNibName:@"SortView3Cell" bundle:nil] forCellWithReuseIdentifier:@"SortView3Cell"];

    //注册header(或fooder 不需要的可以不用)

    [_collectionView registerNib:[UINib nibWithNibName:@"SoryHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SoryHeaderView"];

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

    //cell大小设置

    layOut.itemSize = CGSizeMake( (WIDTH - 82)/3, 30);

    //header大小设置

    layOut.headerReferenceSize = CGSizeMake( WIDTH - 82, 30);

    //cell横向之间的间隔,此属性如果不设置,itemSize设置cell之间一定会有间隔

    layOut.minimumInteritemSpacing = 0.0;

    //cell竖向之间的距离

    layOut.minimumLineSpacing = 0.0;

    //section的边距

    //layOut.sectionInset = UIEdgeInsetsMake(5, 0, 5, 0);

    _collectionView.collectionViewLayout = layOut;

}

#pragma mark - UICollectionViewDataSource

//Section个数

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 4;

}

//cell数量

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

{

    return 9;

}

//cell创建

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

{

    SortView3Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SortView3Cell" forIndexPath:indexPath];    

    return cell;

}

//header 或fooder创建

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

{

    SoryHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SoryHeaderView" forIndexPath:indexPath];

    return headerView;

}

#pragma mark - UICollectionViewDelegate

//Collection点击事件

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

{

}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath

{  //点击事件响应,这个要设为YES

    return YES;

}

#pragma mark - UICollectionViewDelegateFlowLayout  

//这几个函数与上面viewDidLoad里几个重合,可不设

 //cell大小设置

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

 //section的边距

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

 //cell竖向之间的距离

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;

 //cell横向之间的间隔

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;


如果你觉得对你有帮助,请顶一下










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值