在我們添加購(gòu)物車(chē)的時(shí)候骡送,有時(shí)某些商品的屬性需要進(jìn)行刷選,而且類(lèi)似 Color 和 Size 之間還有多種選擇絮记,此時(shí)會(huì)有可選和可用的狀態(tài)摔踱,類(lèi)似如下。
類(lèi)似淘寶添加時(shí)的屬性
經(jīng)過(guò)小伙伴的研究和提醒怨愤,覺(jué)的我們可以利用兩個(gè)質(zhì)數(shù)之間的積是唯一的派敷,來(lái)實(shí)現(xiàn)。
三種狀態(tài): 選中,不選中篮愉,不可用腐芍。
也就是當(dāng)前同一行有選中和普通的狀態(tài);其他行則是可用和不可用的狀態(tài)试躏。注意看假數(shù)據(jù)構(gòu)成猪勇,這樣就很容易一目了然的
假數(shù)據(jù)
- 具體初次的實(shí)現(xiàn)效果
Demo 演示效果
PS: 下面先造假數(shù)據(jù),直接先實(shí)現(xiàn)颠蕴,現(xiàn)實(shí)中要復(fù)雜很多泣刹。
#import "ViewController.h"
static NSString *const kPQTestCollectionCellIden = @"TestCollectionCellIden";
static const NSInteger kPQTestButtonTag = 100;
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
/**
總的數(shù)據(jù)模型,來(lái)自后臺(tái)
*/
@property (nonatomic, strong) NSArray<NSArray *> *dataArray;
/**
可用的組合積的數(shù)組犀被,來(lái)自后臺(tái)
*/
@property (nonatomic, strong) NSArray *userAvailabilityArray;
/**
選中的積椅您,臨時(shí)變量
*/
@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"]
];
/**
假設(shè)這些選中是 OK 的, 保證每一個(gè)都有的選
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 {
// 對(duì)選中狀態(tài)取反
chooseButton.selected = !chooseButton.selected;
// 獲取 button 的具體信息
NSInteger selectSection = chooseButton.tag / kPQTestButtonTag - 1;
NSInteger selectRow = chooseButton.tag % kPQTestButtonTag - 1;
if (chooseButton.selected) {
// 選中的狀態(tài)
[self selectSomeButtonWithSection:selectSection row:selectRow chooseButtonTag:chooseButton.tag];
}
else {
// 取反的狀態(tài)
[self unSelectSomeButtonWithSection:selectSection row:selectRow chooseButtonTag:chooseButton.tag];
}
}
/**
選中某個(gè)
*/
- (void)selectSomeButtonWithSection:(NSInteger)selectSection row:(NSInteger)selectRow chooseButtonTag:(NSInteger)chooseButtonTag {
// 先判斷如果是同一行的話
[self.dataArray enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger sectionIdx, BOOL * _Nonnull sectionStop) {
if (selectSection == sectionIdx) {
// 對(duì)同一種類(lèi)型進(jìn)行互斥
[self.dataArray[selectSection] enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull stop) {
// 獲取與之對(duì)應(yīng)的 Cell 中的 Button
UIButton *button = [self getCellWithSection:selectSection row:rowIdx];
// 假如是同一行的其他標(biāo)簽,而且是選中狀態(tài)
if (button.tag != chooseButtonTag && button.selected) {
// 將上一個(gè)選中設(shè)置為普通
button.selected = NO;
// 同時(shí)移除上一次的選中的值
[self.chooseSelectArray removeObject:self.dataArray[selectSection][rowIdx]];
}
}];
*sectionStop = YES;
}
}];
// 再判斷其他種行種類(lèi)的, 是否 可用
[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) {
// 獲取與之對(duì)應(yīng)的 Cell 中的 Button
UIButton *button = [self getCellWithSection:sectionIdx row:rowIdx];
// 是否可用
button.enabled = ([useStr integerValue] % [obj integerValue] == 0);
if (button.enabled) {
* useStop = YES;
}
}];
}];
}
}];
}
/**
取反某個(gè)
*/
- (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) {
// 獲取與之對(duì)應(yīng)的 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) {
// 獲取與之對(duì)應(yīng)的 Cell 中的 Button
UIButton *button = [self getCellWithSection:selectSection row:rowIdx];
// 假如是其他標(biāo)簽掀泳,而且是選中狀態(tài)
if (button.tag == chooseButtonTag && !button.selected) {
// 同時(shí)移除上一次的選中的值
[self.chooseSelectArray removeObject:self.dataArray[selectSection][rowIdx]];
*rowStop = YES;
}
}];
}
}];
}
/**
獲取相對(duì)應(yīng)的 Cell 中的 Button 按鈕
*/
- (UIButton *)getCellWithSection:(NSInteger)section row:(NSInteger)row {
// 獲取與之對(duì)應(yīng)的 Cell
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];
// 獲取與之對(duì)應(yīng)的 Button
UIButton *button = (UIButton *)[cell viewWithTag:(kPQTestButtonTag * (section + 1) + (row + 1))];
return button;
}
/**
獲取已經(jīng)選中的 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;
}
/**
獲取可用的 數(shù)組 Value
*/
- (NSArray *)getCanUseArrayWithSelect:(BOOL)select {
long long chooseAllValue = [self getChooseSelectValue];
/**
chooseAllValue == 1 表示為空
self.chooseSelectArray.count == 1 && !select: 取反時(shí)只剩下一個(gè)的時(shí)候
*/
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; // 此處先寫(xiě)死,size 先設(shè)置個(gè) 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 是直接用質(zhì)數(shù)代替顯示的员舵,真實(shí)中需要通過(guò) Model
來(lái)配合,另外以下幾點(diǎn)需要注意下:
- 核心是任何兩個(gè)質(zhì)數(shù)的積是唯一的T馍獭9塘椤!
- 現(xiàn)實(shí)中注意和后臺(tái)數(shù)據(jù)的協(xié)商劫流,Model 組合怎樣最方便巫玻,這個(gè)可以好好商量。
- 注意反選時(shí)祠汇,一定要多加測(cè)試仍秤,這里很容易留下坑。
當(dāng)然上面 Demo 中的遍歷很多可很,還可以很多優(yōu)化诗力,算法此處可優(yōu)化的空間很大,歡迎指導(dǎo)我抠。
由于上述仍然有問(wèn)題苇本,不夠嚴(yán)謹(jǐn),在不優(yōu)先考慮算法的前提下菜拓,繼續(xù)優(yōu)化瓣窄。
之前的思路是:
- 同行, 做置反效果纳鼎,就是選中和不選中的排序 (
select
和normal
的區(qū)別) - 其他行俺夕,拿到選中的乘積看是否被整除否 (
enable
和able
的區(qū)別)
現(xiàn)在的思路是:
正選: 添加
- 先同一行:置反裳凸,這個(gè)需要單獨(dú)提出來(lái)。
- 其他行:先判斷此行有沒(méi)有被選中劝贸,默認(rèn)被選中的值 為 1(
hadSelectValue
)姨谷, 然后已經(jīng)選中的乘積 (allValue
),當(dāng)前選中的值的 (nowItemValue
)映九。
(allValue
/hadSelectValue
*nowItemValue
) 看是否可以被整除梦湘?
反選:移除
- 同一行: 置反
- 其他行:同正選一樣。
改進(jìn)一氯迂、邏輯優(yōu)化
效果二
/**
選中某個(gè)
*/
- (void)selectSomeButtonWithSection:(NSInteger)selectSection row:(NSInteger)selectRow chooseButtonTag:(NSInteger)chooseButtonTag {
// 先判斷如果是同一行的話
[self.dataArray enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger sectionIdx, BOOL * _Nonnull sectionStop) {
if (selectSection == sectionIdx) {
// 對(duì)同一種類(lèi)型進(jìn)行互斥
[self.dataArray[selectSection] enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger rowIdx, BOOL * _Nonnull stop) {
// 獲取與之對(duì)應(yīng)的 Cell 中的 Button
UIButton *button = [self getCellWithSection:selectSection row:rowIdx];
// 假如是同一行的其他標(biāo)簽践叠,而且是選中狀態(tài)
if (button.tag != chooseButtonTag && button.selected) {
// 將上一個(gè)選中設(shè)置為普通
button.selected = NO;
// 同時(shí)移除上一次的選中的值
[self.chooseSelectArray removeObject:self.dataArray[selectSection][rowIdx]];
}
}];
*sectionStop = YES;
}
}];
// 再判斷其他種行種類(lèi)的, 是否 可用
[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) {
// 獲取與之對(duì)應(yīng)的 Cell 中的 Button
UIButton *button = [self getCellWithSection:sectionIdx row:rowIdx];
// 獲取當(dāng)前的 Value
NSInteger buttonValueInter = [self.dataArray[sectionIdx][rowIdx] integerValue];
// 就是沒(méi)有選中
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;
}
}];
}
}];
}
}];
}
/**
取反某個(gè)
*/
- (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) {
// 獲取與之對(duì)應(yīng)的 Cell 中的 Button
UIButton *button = [self getCellWithSection:sectionIdx row:rowIdx];
// 獲取當(dāng)前的 Value
NSInteger buttonValueInter = [self.dataArray[sectionIdx][rowIdx] integerValue];
// 就是沒(méi)有選中
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啦嚼蚀,算法的復(fù)雜度還很高禁灼,繼續(xù)優(yōu)化中。轿曙。弄捕。