設(shè)置布局對象
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = relativeY(32);
layout.minimumInteritemSpacing = relativeY(26);
//設(shè)置滾動(dòng)方向
// layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
CGFloat itemHeight = relativeY(100);
CGFloat itemWidth = relativeY(210);
layout.itemSize = CGSizeMake(itemWidth, itemHeight);
//設(shè)置頭部尺寸
layout.headerReferenceSize=CGSizeMake(SCREEN_WIDTH, relativeY(160));
//設(shè)置底部尺寸
layout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, relativeY(96 + 60));
//創(chuàng)建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;
//注冊相關(guān)的視圖
//可注冊多個(gè)類型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高度應(yīng)該加上header和footer的高度
設(shè)置頭部和底部的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就是頭部 將自定義的頭部視圖內(nèi)容加到header即可
//添加頭視圖的內(nèi)容
[self addContent];
//頭視圖添加view
[header addSubview:self.headerView];
return header;
}
// 如果底部視圖
if([kind isEqualToString:UICollectionElementKindSectionFooter]){
UICollectionReusableView *footer=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerViewIdentifier forIndexPath:indexPath];
//添加頭視圖的內(nèi)容
[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;
......
......
}
- 設(shè)置默認(rèn)選中某個(gè)cell選中,在返回cell方法里面設(shè)置即可
- collectionView: cellForItemAtIndexPath:{
if(........){
cell.selected = YES;
}
}