iOS 中类似 SKU 刷选,利用质数实现

在我们添加购物车的时候,有时某些商品的属性需要进行刷选,而且类似 Color 和 Size 之间还有多种选择,此时会有可选和可用的状态,类似如下。

类似淘宝添加时的属性

经过小伙伴的研究和提醒,觉的我们可以利用两个质数之间的积是唯一的,来实现。

  • 三种状态: 选中,不选中,不可用。
    也就是当前同一行有选中和普通的状态;其他行则是可用和不可用的状态。

  • 注意看假数据构成,这样就很容易一目了然的

假数据
  • 具体初次的实现效果
Demo 演示效果

PS: 下面先造假数据,直接先实现,现实中要复杂很多。

#import "ViewController.h"

static NSString *const kPQTestCollectionCellIden = @"TestCollectionCellIden";
static const NSInteger kPQTestButtonTag = 100;

@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
/**
 总的数据模型,来自后台
 */
@property (nonatomic, strong) NSArray<NSArray *> *dataArray;
/**
 可用的组合积的数组,来自后台
 */
@property (nonatomic, strong) NSArray *userAvailabilityArray;
/**
 选中的积,临时变量
 */
@property (nonatomic, strong) NSMutableArray *chooseSelectArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = @[
                       @[@"2",@"3", @"5"],
                       @[@"7",@"11",@"13",@"17"],
                       @[@"19",@"23",@"29"],
                       @[@"37",@"41", @"43",@"47"]
                       ];
    
    
    /**
     
     假设这些选中是 OK 的, 保证每一个都有的选
     2 * 7 * 19 * 37 = 9842
     2 * 11 * 23 * 37 = 18722
     2 * 17 * 19 * 43 = 27778
     
     3 * 13 * 23 * 43 = 38571
     3 * 17 * 29 * 43 = 63597
     3 * 17 * 29 * 47 = 69513
     3 * 7 * 23 * 47 = 22701
     
     5 * 11 * 19 * 37 = 38665
     5 * 17 * 23 * 43 = 84065
     5 * 7 * 23 * 41 = 33005
     5 * 17 * 29 * 47 = 115855
     
     
     */
    self.userAvailabilityArray = @[@"9842",@"18722",@"27778",@"38571",@"63597",@"69513",@"22701",@"38665",@"33005",@"84065",@"115855"];
    
    [self.collectionView reloadData];
    
}

- (void)changeButtonStatus:(UIButton *)chooseButton {
    // 对选中状态取反
    chooseButton.selected = !chooseButton.selected;
    // 获取 button 的具体信息
    NSInteger selectSection = chooseButton.tag / kPQTestButtonTag  - 1;
    NSInteger selectRow = chooseButton.tag % kPQTestButtonTag - 1;
    
    if (chooseButton.selected) {
        // 选中的状态
        [self selectSomeButtonWithSection:selectSection row:selectRow chooseButtonTag:chooseButton.tag];
    }
    else {
        // 取反的状态
        [self unSelectSomeButtonWithSection:selectSection row:selectRow chooseButtonTag:chooseButton.tag];
    }
}

/**
 选中某个
 */
- (void)selectSomeButtonWithSection:(NSInteger)selectSection row:(NSInteger)selectRow chooseButtonTag:(NSInteger)chooseButtonTag {
    // 先判断如果是同一行的话
    [self.dataArray enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger sectionIdx, BOOL * _Nonnull sectionStop) {
        if (selectSection == sectionIdx) {
            // 对同一种类型进行互斥
            [self.dataArray[selectSection] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull stop) {
                // 获取与之对应的 Cell 中的 Button
                UIButton *button = [self getCellWithSection:selectSection row:rowIdx];
                // 假如是同一行的其他标签,而且是选中状态
                if (button.tag != chooseButtonTag && button.selected) {
                    // 将上一个选中设置为普通
                    button.selected = NO;
                    // 同时移除上一次的选中的值
                    [self.chooseSelectArray removeObject:self.dataArray[selectSection][rowIdx]];
                }
            }];
            *sectionStop = YES;
        }
    }];
    // 再判断其他种行种类的, 是否 可用
    [self.chooseSelectArray addObject:self.dataArray[selectSection][selectRow]];
    NSArray *useArray = [self getCanUseArrayWithSelect:YES];
    [self.dataArray enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger sectionIdx, BOOL * _Nonnull stop) {
        if (selectSection != sectionIdx) {
            // 不是选中的这一行,则刷新 是否可用
            [self.dataArray[sectionIdx] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
                [useArray enumerateObjectsUsingBlock:^(NSString *useStr, NSUInteger useIdx, BOOL * _Nonnull useStop) {
                    // 获取与之对应的 Cell 中的 Button
                    UIButton *button = [self getCellWithSection:sectionIdx row:rowIdx];
                    // 是否可用
                    button.enabled = ([useStr integerValue] % [obj integerValue] == 0);
                    if (button.enabled) {
                        * useStop = YES;
                    }
                }];
            }];
        }
    }];
}

/**
 取反某个
 */
- (void)unSelectSomeButtonWithSection:(NSInteger)selectSection row:(NSInteger)selectRow chooseButtonTag:(NSInteger)chooseButtonTag {
    [self.chooseSelectArray removeObject:self.dataArray[selectSection][selectRow]];
    NSArray *useArray = [self getCanUseArrayWithSelect:NO];
    [self.dataArray enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger sectionIdx, BOOL * _Nonnull stop) {
        if (selectSection != sectionIdx) {
            [self.dataArray[sectionIdx] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
                [useArray enumerateObjectsUsingBlock:^(NSString *useStr, NSUInteger useIdx, BOOL * _Nonnull useStop) {
                    // 获取与之对应的 Cell 中的 Button
                    UIButton *button = [self getCellWithSection:sectionIdx row:rowIdx];
                    // 是否可用
                    button.enabled = ([useStr integerValue] % [obj integerValue] == 0);
                    if (button.enabled) {
                        * useStop = YES;
                    }
                }];
            }];
        }
        else {
            [self.dataArray[selectSection] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
                // 获取与之对应的 Cell 中的 Button
                UIButton *button = [self getCellWithSection:selectSection row:rowIdx];
                // 假如是其他标签,而且是选中状态
                if (button.tag == chooseButtonTag && !button.selected) {
                    // 同时移除上一次的选中的值
                    [self.chooseSelectArray removeObject:self.dataArray[selectSection][rowIdx]];
                    *rowStop = YES;
                }
            }];
        }
    }];
    
}

/**
 获取相对应的 Cell 中的 Button 按钮
 */
- (UIButton *)getCellWithSection:(NSInteger)section row:(NSInteger)row {
    // 获取与之对应的 Cell
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
    // 获取与之对应的 Button
    UIButton *button = (UIButton *)[cell viewWithTag:(kPQTestButtonTag * (section + 1) + (row + 1))];
    return button;
}


/**
 获取已经选中的 Value 乘积
 */
- (long long)getChooseSelectValue {
    __block long long hadChooseValue = 1;
    [self.chooseSelectArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        hadChooseValue *= [obj integerValue];
    }];
    return hadChooseValue;
}

/**
 获取可用的 数组 Value
 */
- (NSArray *)getCanUseArrayWithSelect:(BOOL)select {
    long long chooseAllValue = [self getChooseSelectValue];
    /**
     chooseAllValue == 1 表示为空
     self.chooseSelectArray.count == 1 && !select: 取反时只剩下一个的时候
     */
    if (chooseAllValue == 1 || (self.chooseSelectArray.count == 1 && !select)) {
        return self.userAvailabilityArray;
    }
    NSMutableArray *useArray = [NSMutableArray array];
    [self.userAvailabilityArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger useInteger = [obj integerValue];
        if ((useInteger % chooseAllValue) == 0) {
            [useArray addObject:obj];
        }
    }];
    return useArray.copy;
}

#pragma mark - Delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return  self.dataArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray[section].count;;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPQTestCollectionCellIden forIndexPath:indexPath];
    // 显示 �UIButton
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = cell.bounds; // 此处先写死,size 先设置个 60、50
    [button setTitle:(self.dataArray[indexPath.section][indexPath.row]) forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button pq_setBackgroundColor:[UIColor orangeColor] state:UIControlStateSelected];
    [button pq_setBackgroundColor:[UIColor blackColor] state:UIControlStateNormal];
    [button pq_setBackgroundColor:[UIColor grayColor] state:UIControlStateDisabled];
    button.tag = kPQTestButtonTag * (indexPath.section + 1) + (indexPath.row + 1);
    [button addTarget:self action:@selector(changeButtonStatus:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:button];
    
    return cell;
}

#pragma mark - Getter
- (NSMutableArray *)chooseSelectArray {
    if (!_chooseSelectArray) {
        _chooseSelectArray = [NSMutableArray array];
    }
    return _chooseSelectArray;
}

@end

@implementation UIButton (PQBackgroundColor)

- (void)pq_setBackgroundColor:(UIColor *)color state:(UIControlState)state {
    [self setBackgroundImage:[self pq_imageWithColor:color] forState:state];
}

- (UIImage *)pq_imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end

以上 Demo 是直接用质数代替显示的,真实中需要通过 Model 来配合,另外以下几点需要注意下:

  • 核心是任何两个质数的积是唯一的!!!
  • 现实中注意和后台数据的协商,Model 组合怎样最方便,这个可以好好商量。
  • 注意反选时,一定要多加测试,这里很容易留下坑。

当然上面 Demo 中的遍历很多,还可以很多优化,算法此处可优化的空间很大,欢迎指导。

由于上述仍然有问题,不够严谨,在不优先考虑算法的前提下,继续优化。

之前的思路是:

  • 同行, 做置反效果,就是选中和不选中的排序 (selectnormal的区别)
  • 其他行,拿到选中的乘积看是否被整除否 (enableable 的区别)

现在的思路是:

正选: 添加

  • 先同一行:置反,这个需要单独提出来。
  • 其他行:先判断此行有没有被选中,默认被选中的值 为 1(hadSelectValue), 然后已经选中的乘积 (allValue),当前选中的值的 (nowItemValue)。
    allValue / hadSelectValue * nowItemValue) 看是否可以被整除?

反选:移除

  • 同一行: 置反
  • 其他行:同正选一样。
改进一、逻辑优化
效果二
/**
 选中某个
 */
- (void)selectSomeButtonWithSection:(NSInteger)selectSection row:(NSInteger)selectRow chooseButtonTag:(NSInteger)chooseButtonTag {
    // 先判断如果是同一行的话
    [self.dataArray enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger sectionIdx, BOOL * _Nonnull sectionStop) {
        if (selectSection == sectionIdx) {
            // 对同一种类型进行互斥
            [self.dataArray[selectSection] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull stop) {
                // 获取与之对应的 Cell 中的 Button
                UIButton *button = [self getCellWithSection:selectSection row:rowIdx];
                // 假如是同一行的其他标签,而且是选中状态
                if (button.tag != chooseButtonTag && button.selected) {
                    // 将上一个选中设置为普通
                    button.selected = NO;
                    // 同时移除上一次的选中的值
                    [self.chooseSelectArray removeObject:self.dataArray[selectSection][rowIdx]];
                }
            }];
            *sectionStop = YES;
        }
    }];
    // 再判断其他种行种类的, 是否 可用
    [self.chooseSelectArray addObject:self.dataArray[selectSection][selectRow]];
    NSInteger allValueIntger = [self getChooseSelectValue];
    [self.dataArray enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger sectionIdx, BOOL * _Nonnull stop) {
        if (selectSection != sectionIdx) {
            // 不是选中的这一行,则刷新 是否可用
            __block NSInteger hadSelectValue = 1;
            if (self.chooseSelectArray.count > 1) {
                [self.dataArray[sectionIdx] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
                    if ([self.chooseSelectArray containsObject:obj]) {
                        hadSelectValue = [obj integerValue];
                        *rowStop = YES;
                    }
                }];
            }
            [self.dataArray[sectionIdx] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
                // 获取与之对应的 Cell 中的 Button
                UIButton *button = [self getCellWithSection:sectionIdx row:rowIdx];
                // 获取当前的 Value
                NSInteger buttonValueInter = [self.dataArray[sectionIdx][rowIdx] integerValue];
                // 就是没有选中
                if (!button.selected) {
                    [self.userAvailabilityArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        button.enabled = NO;
                        if (([obj integerValue] % (allValueIntger * buttonValueInter / hadSelectValue)) == 0) {
                            button.enabled = YES;
                            *stop = YES;
                        }
                    }];
                }
            }];
        }
    }];
}

/**
 取反某个
 */
- (void)unSelectSomeButtonWithSection:(NSInteger)selectSection row:(NSInteger)selectRow chooseButtonTag:(NSInteger)chooseButtonTag {
    [self.chooseSelectArray removeObject:self.dataArray[selectSection][selectRow]];
    NSInteger allValueIntger = [self getChooseSelectValue];
    [self.dataArray enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger sectionIdx, BOOL * _Nonnull stop) {
        if (selectSection != sectionIdx) {
            [self.dataArray[sectionIdx] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
                __block NSInteger hadSelectValue = 1;
                [self.dataArray[sectionIdx] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
                    if ([self.chooseSelectArray containsObject:obj]) {
                        hadSelectValue = [obj integerValue];
                        *rowStop = YES;
                    }
                }];
                [self.dataArray[sectionIdx] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull rowStop) {
                    // 获取与之对应的 Cell 中的 Button
                    UIButton *button = [self getCellWithSection:sectionIdx row:rowIdx];
                    // 获取当前的 Value
                    NSInteger buttonValueInter = [self.dataArray[sectionIdx][rowIdx] integerValue];
                    // 就是没有选中
                    if (!button.selected) {
                        [self.userAvailabilityArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            button.enabled = NO;
                            if (([obj integerValue] % (allValueIntger / hadSelectValue * buttonValueInter)) == 0) {
                                button.enabled = YES;
                                *stop = YES;
                            }
                        }];
                    }
                }];

            }];
        }
     }];
}

目前逻辑基本OK啦,算法的复杂度还很高,继续优化中。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 小升初的过程中,竞赛成绩能起到相当大的作用,谈到竞赛就离不开奥数。以下是小学奥数题知识点大汇总: 1.和差倍问题 ...
    沪江中小幼阅读 1,144评论 0 7
  • 小学奥数其实很简单,以下是这六个部分的知识点! 1 第一部分(知识点1-6) 2、年龄问题的三个基本特征: ①两个...
    小一哥阅读 1,360评论 0 3
  • 你既然不愿意低下头来亲我,那我就踮起脚尖去吻你。 美好的爱情总是让人眷恋,有多少的缘分是在我们踮起脚尖吻向心爱之...
    诗来阅读 448评论 0 0
  • 走在五月的街道,被太阳晒的无处躲藏,满街都是飘逸的纱裙和凉拖鞋,气温也高的让人退避三舍想窝在房间里吹凉风。我知...
    赤心心赤阅读 424评论 0 0
  • 文/新鲜 之前有人问说:你愿意和现在的自己谈恋爱吗? 1.今天家庭视频,表哥说你气色这么好是不是恋爱了?我心理OS...
    新鲜wendy阅读 302评论 0 0