iOS接下来要研究的知识点

iOS 商品规格选择View

2019-05-21  本文已影响0人  三千沉浮
最近做了两三个商城类的项目,由于时间匆忙,商品规格选择的View都是网上找的Demo,用起来总归不是那么顺手,这两天抽时间自己写了一个。
废话不说,先上效果图: 商品规格选择.png
说一下主要思路,规格选择View分为三个部分,上部分的商品信息view,中间部分的商品规格view,是一个collectionView,最下边是一个商品数量和确定按钮view。下面贴出部分关键代码,主要分为两部分。

1、点击规格的时候往选中规格数组中添加规格model,且同一级只能有一个model放入数组。

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    for (int i = 0; i<self.array_dataSource.count; i++) {
        if (indexPath.section == i) {
            LQStandardModel *model = self.array_dataSource[i];
            [model.proArrs enumerateObjectsUsingBlock:^(LQStandardSonModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (idx == indexPath.item) {
                    obj.isSelected = !obj.isSelected;
                    if (obj.isSelected) {
                        [self.array_selectedModel addObject:obj];
                    }else{
                        [self.array_selectedModel removeObject:obj];
                    }
                }else{
                    obj.isSelected = NO;
                    [self.array_selectedModel removeObject:obj];
                }
            }];
        }
    }
    
    [UIView performWithoutAnimation:^{
        //刷新界面
        [self.collectionView reloadData];
    }];
    
    [self getProStndardMsg];
    
}

2、用冒泡排序排列选中model数组中的model,让model放入一一对应的位置。

-(void)getProStndardMsg{
    if (self.array_selectedModel.count == self.array_dataSource.count) {
        NSUInteger count = self.array_selectedModel.count;
        for (int i=0; i<count-1; i++) {
            for (int j=0; j<count-1-i; j++) {
                if (self.array_selectedModel[j].proIdx > self.array_selectedModel[j+1].proIdx) {
                    [self.array_selectedModel exchangeObjectAtIndex:j withObjectAtIndex:j+1];
                }
            }
        }
        NSArray *arr_proStandardName = [self.array_selectedModel valueForKeyPath:@"proStandardName"];
        NSArray *arr_proStandardTitleId = [self.array_dataSource valueForKey:@"proStandardTitleId"];
        NSArray *arr_ids = [self.array_selectedModel valueForKeyPath:@"proStandardNameId"];
        NSMutableArray *array_ids = [[NSMutableArray alloc]initWithCapacity:0];
        for (int i = 0; i<arr_proStandardTitleId.count; i++) {
            NSString *modelId = arr_proStandardTitleId[i];
            NSString *modelSonId = arr_ids[i];
            NSString *ids = [NSString stringWithFormat:@"%@:%@",modelId,modelSonId];
            [array_ids addObject:ids];
        }
        if (self.proStandard) {
            self.proStandard([arr_proStandardName componentsJoinedByString:@":"], [array_ids componentsJoinedByString:@";"],YES);
        }
    }else{
        self.proStandard(@"", @"",NO);
    }
    
}

另外还有一点比较重要的是collectionViewCell的大小自适应以及两个cell之间的间距固定问题,间距固定问题需要自定义UICollectionViewLayout,而collectionViewCell大小自定义代码如下

-(void)configurationUI{
    
    UILabel *lab_standard = [LQCustomControl createLabelWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) text:@"规格" bgColor:[UIColor lightGrayColor] textColor:[UIColor blackColor] font:FontThin(15.0*WidthRatio) textAlignment:NSTextAlignmentCenter];
    lab_standard.layer.masksToBounds = YES;
    lab_standard.layer.cornerRadius = 3*WidthRatio;
    [self addSubview:lab_standard];
    self.lab_standard = lab_standard;
    
}

-(void)setModel:(LQStandardSonModel *)model{
    _model = model;
//这句代码很重要,不写会造成刷新collectionView的时候cell大小发生变化
    self.lab_standard.frame = CGRectMake(0, 0, model.itemWidth, 30*WidthRatio);
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //设置cell的大小
    LQStandardModel *model = self.array_dataSource[indexPath.section];
    LQStandardSonModel *model_son = model.proArrs[indexPath.item];
    return CGSizeMake(model_son.itemWidth, 30*WidthRatio);
}

大致的逻辑就是这个样子的,最后附上Demo链接https://github.com/MayXiaoYang/LQProStandardView

如果有用,点个赞再走啊~

上一篇下一篇

猜你喜欢

热点阅读