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;
如果你觉得对你有帮助,请顶一下