工作生活

iOS使用SB+xib创建collectionView视图

2019-07-03  本文已影响0人  Double丶K
1.建立SB
image.png
2.拖拽属性,完成代理实现
UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];
    flowLaout.scrollDirection = UICollectionViewScrollDirectionVertical;
//上下间距
    flowLaout.minimumLineSpacing = 0;
//左右间距
    flowLaout.minimumInteritemSpacing = 0;
    flowLaout.itemSize = CGSizeMake((ScreenWidth-55)/2, Width(243));
    _collectionView.collectionViewLayout = flowLaout;
//注册xib建的cell
    [_collectionView registerNib:[UINib nibWithNibName:@"FDStoreGoodsCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"FDStoreGoodsCollectionViewCell"];

//注册尾视图
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
    //注册xib建的头视图
    [_collectionView registerNib:[UINib nibWithNibName:@"FDShopHeardView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDShopHeardView"];

实现代理方法

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 20;
}

//内边距处理
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // UIEdgeInsets insets = {top, left, bottom, right};
    return UIEdgeInsetsMake(0, 20, 0, 20);
}



//设置尾部视图高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(ScreenWidth, 50);
    
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(ScreenWidth,130);
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FDStoreGoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDStoreGoodsCollectionViewCell" forIndexPath:indexPath];
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if (kind == UICollectionElementKindSectionHeader) {
        FDShopHeardView *header = (FDShopHeardView*)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDShopHeardView" forIndexPath:indexPath];
        return header;
    }else{
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor whiteColor];
        for (UIView *view in footerView.subviews) { [view removeFromSuperview]; }
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake((ScreenWidth)/2-80, 20, 160, 17)];
        titleLabel.textAlignment =  NSTextAlignmentCenter;
        titleLabel.text = @"更多商品敬请期待…";
        [footerView addSubview:titleLabel];
        return footerView;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}
上一篇 下一篇

猜你喜欢

热点阅读