设置布局对象
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = relativeY(32);
layout.minimumInteritemSpacing = relativeY(26);
//设置滚动方向
// layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
CGFloat itemHeight = relativeY(100);
CGFloat itemWidth = relativeY(210);
layout.itemSize = CGSizeMake(itemWidth, itemHeight);
//设置头部尺寸
layout.headerReferenceSize=CGSizeMake(SCREEN_WIDTH, relativeY(160));
//设置底部尺寸
layout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, relativeY(96 + 60));
//创建collectionView
UICollectionView * exchangeView = [[UICollectionView alloc] initWithFrame:CGRectMake(relativeY(32), relativeY(248), SCREEN_WIDTH - relativeY(64), relativeY(60 + 100 + 32 + 100 + 60)) collectionViewLayout:layout];
self.exchangeView = exchangeView;
[self.view addSubview:exchangeView];
exchangeView.backgroundColor = [UIColor greenColor];
exchangeView.delegate = self;
exchangeView.dataSource = self;
//注册相关的视图
//可注册多个类型cell
[exchangeView registerClass:[DWHWSBToDiamondCell class] forCellWithReuseIdentifier:WSBToDiamond];
[exchangeView registerClass:[DWHWSBToRMBCell class] forCellWithReuseIdentifier:WSBToRMB];
//注册头部视图和底部视图
[exchangeView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];
[exchangeView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerViewIdentifier];
❗️ collectionView的frame高度应该加上header和footer的高度
设置头部和底部的view
// 返回头视图 底视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//如果是头视图
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath];
//❗️header就是头部 将自定义的头部视图内容加到header即可
//添加头视图的内容
[self addContent];
//头视图添加view
[header addSubview:self.headerView];
return header;
}
// 如果底部视图
if([kind isEqualToString:UICollectionElementKindSectionFooter]){
UICollectionReusableView *footer=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerViewIdentifier forIndexPath:indexPath];
//添加头视图的内容
[self configureFooter];
//头视图添加view
[footer addSubview:self.footer];
return footer;
}
return nil;
}
自定义CollectionViewCell
#import <UIKit/UIKit.h>
#import "DWHWSBToRMBModel.h"
@interface DWHWSBToRMBCell : UICollectionViewCell
@property (nonatomic,strong) DWHWSBToRMBModel * model;
@end
#import "DWHWSBToRMBCell.h"
@interface DWHWSBToRMBCell ()
@property (nonatomic ,strong ) UILabel * wsbLabel;
@property (nonatomic ,strong ) UILabel * rmbLabel;
@end
@implementation DWHWSBToRMBCell
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self configureUI];
}
return self;
}
- (void)configureUI{
self.contentView.layer.borderWidth = 1;
self.contentView.layer.borderColor = DWHColorFromRGB(0xcccccc).CGColor;
self.contentView.layer.cornerRadius = 10;
self.contentView.layer.masksToBounds = YES;
......
......
}
- 设置默认选中某个cell选中,在返回cell方法里面设置即可
- collectionView: cellForItemAtIndexPath:{
if(........){
cell.selected = YES;
}
}
网友评论