美文网首页
iOS-商品详情规格选择

iOS-商品详情规格选择

作者: 善良的皮蛋 | 来源:发表于2019-07-24 16:42 被阅读0次

前言:各大电商平台都有此类页面,在你选择要购买的商品时候,除了默认的商品规格外,你可以自由选择可供选择的规格,有的商品有多中规格供选择,今天分享的是当前项目中使用的一种方法,仅供参考,不足之处,欢迎指出。

先看效果图
image.png

看到这种多选的规格,第一反应就是collectionview,这个时候关键问题就出来了,规格的内容长度并不是一样的,但是系统默认的cell的宽度却只能设置成一样的,这个时候大家都知道接下来我要说的了,自定义CollectionviewFlowLayout,是的。

@protocol LeeIrregularCollectionViewFlowLayoutDelegate <NSObject>

@required

// cell size
- (CGSize)LeeIrregularCollectionViewFlowLayout:(UICollectionViewLayout *)layout itemSizeForIndexPath:(NSIndexPath *)indexPath;

@optional

// headerview size
- (CGSize)LeeIrregularCollectionViewFlowLayout:(UICollectionViewLayout *)layout headerViewSizeForIndexPath:(NSIndexPath *)indexPath;

// footerview size
- (CGSize)LeeIrregularCollectionViewFlowLayout:(UICollectionViewLayout *)layout FooterViewSizeForIndexPath:(NSIndexPath *)indexPath;

// 横向间距
- (CGFloat)LeeIrregularCollectionViewFlowLayout:(UICollectionViewLayout*)layout itemMinimumLineSpacingForIndexPath:(NSIndexPath *)indexPath;

// 竖向间距
- (CGFloat)LeeIrregularCollectionViewFlowLayout:(UICollectionViewLayout*)layout ItemMinimumInteritemSpacingForIndexPath:(NSIndexPath *)indexPath;

// 内边距
- (UIEdgeInsets)LeeIrregularCollectionViewFlowLayout:(UICollectionViewLayout*)layout edgeInsetsInWaterflowLayoutForIndexPath:(NSIndexPath *)indexPath;

@end

@interface LeeIrregularCollectionViewFlowLayout : UICollectionViewFlowLayout

@property (nonatomic,weak) id<LeeIrregularCollectionViewFlowLayoutDelegate> leeFlowLayoutdelegate;   //-> 代理
@property (nonatomic,strong) NSMutableArray *attrsArray;        //-> 存放所有 cell headerview footer 的数组
@property (nonatomic,assign) CGFloat contentHeight;             //-> 当前高度
@property (nonatomic,assign) CGFloat currentX;                  //-> 当前cell 右边
@property (nonatomic,assign) CGFloat formerMaxY;                //-> 前一个attr Y 轴 达到的最大位置

@end

@implementation LeeIrregularCollectionViewFlowLayout

#pragma mark - override super method
/**
 * 第一步: 初始化
 */
- (void)prepareLayout{
    [super prepareLayout];
    self.contentHeight = 0.0f;
    self.currentX      = 0.0f;
    self.formerMaxY    = 0.0f;
    
    [self.attrsArray removeAllObjects];
    
    // 如果collectionview 的section == 0 直接returen
    NSInteger sectionNumber = [self.collectionView numberOfSections];
    if (sectionNumber == 0) {
        return;
    }
    
    // section > 0
    for (int i = 0; i < sectionNumber; i++) {
        NSIndexPath *headerFooterIndexPath = [NSIndexPath indexPathForItem:0 inSection:i];
        UICollectionViewLayoutAttributes *sectionHeaderAttri = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:headerFooterIndexPath];
        if (sectionHeaderAttri.frame.size.height > 0) {
            [self.attrsArray addObject:sectionHeaderAttri];
        }
        NSInteger itemNumber = [self.collectionView numberOfItemsInSection:i];
        for (int j = 0; j < itemNumber; j++) {
            NSIndexPath *itemIndexPath = [NSIndexPath indexPathForItem:j inSection:i];
            UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
            NSAssert(attrs.frame.size.height > 0 && attrs.frame.size.width > 0 , @"item size 的高度 或 宽度 为0,这里不允许为0 ");
            if (attrs.frame.size.height > 0 && attrs.frame.size.width > 0) {
                
            }
            [self.attrsArray addObject:attrs];
        }
        
        UICollectionViewLayoutAttributes *sectionFooterAttri = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter atIndexPath:headerFooterIndexPath];
        if (sectionFooterAttri.frame.size.height > 0) {
            [self.attrsArray addObject:sectionFooterAttri];
        }
    }
}

/**
 * 第二步: 决定cell的排布
 */
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    return self.attrsArray;
}

/*!
 *  多次调用 只要滑出范围就会 调用
 *  当CollectionView的显示范围发生改变的时候,是否重新发生布局
 *  一旦重新刷新 布局,就会重新调用
 *  1.layoutAttributesForElementsInRect:方法
 *  2.preparelayout方法
 */
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}


- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
    
    CGSize supplementaryViewSize = CGSizeZero;
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
    
    if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {
        if (self.leeFlowLayoutdelegate && [self.leeFlowLayoutdelegate respondsToSelector:@selector(LeeIrregularCollectionViewFlowLayout:headerViewSizeForIndexPath:)]) {
            supplementaryViewSize = [self.leeFlowLayoutdelegate LeeIrregularCollectionViewFlowLayout:self headerViewSizeForIndexPath:indexPath];
        }
        
    }else if ([elementKind isEqualToString:UICollectionElementKindSectionFooter]){
        if (self.leeFlowLayoutdelegate && [self.leeFlowLayoutdelegate respondsToSelector:@selector(LeeIrregularCollectionViewFlowLayout:FooterViewSizeForIndexPath:)]) {
            supplementaryViewSize = [self.leeFlowLayoutdelegate LeeIrregularCollectionViewFlowLayout:self FooterViewSizeForIndexPath:indexPath];
        }
    }
    
    CGRect frame =  attrs.frame;
    frame.size = supplementaryViewSize;
    frame.origin.y = self.contentHeight;
    attrs.frame = frame;
    self.contentHeight += supplementaryViewSize.height;
    return attrs;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    CGFloat collectionViewW = self.collectionView.frame.size.width;
    CGSize itemSize = [self.leeFlowLayoutdelegate LeeIrregularCollectionViewFlowLayout:self itemSizeForIndexPath:indexPath];
    
    CGFloat itemLineSpacing = 10.0f;
    if (self.leeFlowLayoutdelegate && [self.leeFlowLayoutdelegate respondsToSelector:@selector(LeeIrregularCollectionViewFlowLayout:itemMinimumLineSpacingForIndexPath:)]) {
        itemLineSpacing = [self.leeFlowLayoutdelegate LeeIrregularCollectionViewFlowLayout:self itemMinimumLineSpacingForIndexPath:indexPath];
    }
    CGFloat interItemSpacing = 10.0f;
    if (self.leeFlowLayoutdelegate && [self.leeFlowLayoutdelegate respondsToSelector:@selector(LeeIrregularCollectionViewFlowLayout:ItemMinimumInteritemSpacingForIndexPath:)]) {
        interItemSpacing = [self.leeFlowLayoutdelegate LeeIrregularCollectionViewFlowLayout:self ItemMinimumInteritemSpacingForIndexPath:indexPath];
    }
    UIEdgeInsets sectionEdge =UIEdgeInsetsMake(10.f, 10.f, 10.f, 10.f);
    if (self.leeFlowLayoutdelegate && [self.leeFlowLayoutdelegate respondsToSelector:@selector(LeeIrregularCollectionViewFlowLayout:edgeInsetsInWaterflowLayoutForIndexPath:)]) {
        sectionEdge = [self.leeFlowLayoutdelegate LeeIrregularCollectionViewFlowLayout:self edgeInsetsInWaterflowLayoutForIndexPath:indexPath];
    }
    
    NSInteger itemNumber = [self.collectionView numberOfItemsInSection:indexPath.section];
    if (itemNumber == 0) {
        attrs.frame = CGRectZero;
        return attrs;
    }
    
    CGRect currentFrame = attrs.frame;
    currentFrame.size = itemSize;
    
    CGFloat maxWidth = MIN(currentFrame.size.width, collectionViewW - sectionEdge.left - sectionEdge.right);
    currentFrame.size.width = maxWidth;
    
    if (indexPath.row == 0) {
        self.contentHeight += sectionEdge.top;
        currentFrame.origin.x = sectionEdge.left;
        currentFrame.origin.y = self.contentHeight;
    }else{
        if (self.currentX + itemLineSpacing + itemSize.width + sectionEdge.right <= collectionViewW) { //本行
            currentFrame.origin.x = self.currentX + itemLineSpacing;
            currentFrame.origin.y = self.contentHeight;
        }else{ //下一行
            self.contentHeight = self.formerMaxY + interItemSpacing;
            currentFrame.origin.x = sectionEdge.left;
            currentFrame.origin.y = self.contentHeight;
        }
    }
    
    attrs.frame = currentFrame;
    self.currentX = CGRectGetMaxX(attrs.frame);
    
    self.formerMaxY = MAX(CGRectGetMaxY(attrs.frame), self.formerMaxY);
    if (indexPath.row == (itemNumber -1) ) {
        self.contentHeight  = MAX(CGRectGetMaxY(attrs.frame) + sectionEdge.bottom,  self.formerMaxY + sectionEdge.bottom);
    }
    
    return attrs;
}

- (CGSize)collectionViewContentSize{
    return CGSizeMake(self.collectionView.frame.size.width, self.contentHeight);
}


#pragma mark - setter $ getter
- (NSMutableArray *)attrsArray{
    if (!_attrsArray) {
        _attrsArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _attrsArray;
}

在代理方法中使用

- (CGSize)LeeIrregularCollectionViewFlowLayout:(UICollectionViewLayout *)layout itemSizeForIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake([self widthForString:self.dataArray[indexPath.section][@"list"][indexPath.row][@"name"] fontSize:15 andHeight:50]+20, 50);
}

/** 头部视图Size */
- (CGSize)LeeIrregularCollectionViewFlowLayout:(UICollectionViewLayout *)layout headerViewSizeForIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, 50);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HEADVIEW_ID forIndexPath:indexPath];
        UILabel *label = [[UILabel alloc]init];
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:15];
        [headerView addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(headerView.mas_left).offset(10);
            make.top.bottom.equalTo(headerView);
            make.right.equalTo(headerView.mas_right).offset(-10);
        }];
        
        label.text = self.dataArray[indexPath.section][@"title"];
        
        return headerView;
    }
    return nil;
}

在collectionview 懒加载方法中使用

 LeeIrregularCollectionViewFlowLayout *flowLaout = [[LeeIrregularCollectionViewFlowLayout alloc]init];
        flowLaout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _baseCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLaout];
        flowLaout.leeFlowLayoutdelegate = self;
 

大功告成 效果如下

image.png

相关文章

  • iOS-商品详情规格选择

    前言:各大电商平台都有此类页面,在你选择要购买的商品时候,除了默认的商品规格外,你可以自由选择可供选择的规格,有的...

  • 商品详情-规格

    商品基础数据参数说明 参数名类型说明goods_snstring商品编号goods_namestring商品标题g...

  • 质数在商品规格选择中的应用

    向淘宝京东商品选择的时候,会有规格选择,多种规格联动,如下三个截图淘宝的商品规格选择,在有的规格下没有商品或没货需...

  • 【产品常识】商品详情页面结构

    商品详情页面结构: 1、商品宣传图 2、商品名 3、原价现价 4、类别规格选项 5、购买按钮 6、长篇幅的图片详情...

  • Android模仿京东商品详情 选择规格特效

    最近我的5s实在卡的不行就打算买个手机,在京东上看手机的时候发现这个效果,感觉挺好玩的,就想着自己实现下,iOS版...

  • 商品基本要素(摘自电商产品经理宝典)

    商品基本要素 类目、品牌、标题、商品属性、规格、价格、库存、SKU信息、商品图、商品详情描述、物流信息、商品信息标...

  • 商品详情存储方案拆分

    商品详情按照业务粒度划分为介绍,规格,包装售后....分别存储,标记为small(v),同时它们为一个商品详情类的...

  • Swisse Ultiboost Immune 提高免疫力保健片

    商品详情 【商品中文名称】Swisse Ultiboost Immune提高免疫力保健片 【产地】澳大利亚 【规格...

  • wepy小程序项目

    WePY开发文档参考 店铺 店铺首页 店铺信息 商品 分类展示 规格展现 购物车 商品详情 会员折扣价 订单 商品...

  • iOS 选择商品规格

    由实现选规格的效果来记录下所用到的基础控件和知识点 控件: UIcollectionview 初始化 collec...

网友评论

      本文标题:iOS-商品详情规格选择

      本文链接:https://www.haomeiwen.com/subject/olxfrctx.html