本节内容主要为纯代码方式使用UICollectionView
1.首先是UICollectionView的创建
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];//这里的layout主要是对collectionView的布局。
layout.footerReferenceSize =CGSizeMake(SCREEN_WIDTH,5);//footerReferenceSize对全局的collectionView的footer尺寸进行设定还有与其对应的headerReferenceSize负责设置全局的header的尺寸
//layout.scrollDirection默认是垂直滚动
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
//注册WLGNewsGoodsCommendCell因为系统没有默认的UICollecitonCell进行布局这里是继承自UICollecitonCell进行的布局
[_collectionView registerClass:[WLGNewsGoodsCommendCell class]
forCellWithReuseIdentifier:@"WLGNewsGoodsCommendCell"];
//注册HeaderView
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
/注册FooterView
[_collectionView registerClass:[UICollectionReusableView class]
forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
_collectionView.delegate =self;
_collectionView.dataSource =self;
2.代理方法 数据代理方法:UICollectionViewDataSource
//必须实现的两种
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//设置分区的cell数量
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
WLGNewsGoodsCommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WLGNewsGoodsCommendCell"
forIndexPath:indexPath];
//设置cell此前要对WLGNewsGoodsCommendCell进行注册见UICollectionView的创建
}
//选择实现
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return2;//此方法不实现,默认返回1个分区。
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath;
{
UICollectionReusableView *reusableView = nil;
if (kind ==UICollectionElementKindSectionHeader) {
UICollectionReusableView *view = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"HeaderView"forIndexPath:indexPath];
reusableView = view;
}
if (kind ==UICollectionElementKindSectionFooter) {
UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"forIndexPath:indexPath];
reusableView = view;
}
return reusableView;//同样这里也要对 HeaderView和 FooterView进行注册见UICollectionView的创建
}
3.布局代理 UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//对每一个cell的尺寸进行设置的方法对应全局的 itemSize属性
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayoutminimumLineSpacingForSectionAtIndex:(NSInteger)section
{
//对每个分区的行间距进行单独设置对应全局的 minimumLineSpacing属性
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayoutminimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
//对每个分区的列间距进行单独设置对应全局的 minimumInteritemSpacing属性
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayoutreferenceSizeForHeaderInSection:(NSInteger)section
{
//对每个分区头尺寸单独设置对应全局的 headerReferenceSize属性
}
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayoutreferenceSizeForFooterInSection:(NSInteger)section
{
//对每个分区的尾尺寸单独设置对应全局的 footerReferenceSize属性
}
4.操作方法的 代理 UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//选中具体cell的操作方法
}