最近做了兩三個商城類的項目琢岩,由于時間匆忙辣往,商品規(guī)格選擇的View都是網(wǎng)上找的Demo市栗,用起來總歸不是那么順手沈贝,這兩天抽時間自己寫了一個。
廢話不說踩晶,先上效果圖:
說一下主要思路执泰,規(guī)格選擇View分為三個部分,上部分的商品信息view渡蜻,中間部分的商品規(guī)格view术吝,是一個collectionView,最下邊是一個商品數(shù)量和確定按鈕view。下面貼出部分關(guān)鍵代碼茸苇,主要分為兩部分排苍。
1、點(diǎn)擊規(guī)格的時候往選中規(guī)格數(shù)組中添加規(guī)格model学密,且同一級只能有一個model放入數(shù)組淘衙。
-(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數(shù)組中的model腻暮,讓model放入一一對應(yīng)的位置彤守。
-(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);
}
}
另外還有一點(diǎn)比較重要的是collectionViewCell的大小自適應(yīng)以及兩個cell之間的間距固定問題,間距固定問題需要自定義UICollectionViewLayout,而collectionViewCell大小自定義代碼如下
-(void)configurationUI{
UILabel *lab_standard = [LQCustomControl createLabelWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) text:@"規(guī)格" 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大小發(fā)生變化
self.lab_standard.frame = CGRectMake(0, 0, model.itemWidth, 30*WidthRatio);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//設(shè)置cell的大小
LQStandardModel *model = self.array_dataSource[indexPath.section];
LQStandardSonModel *model_son = model.proArrs[indexPath.item];
return CGSizeMake(model_son.itemWidth, 30*WidthRatio);
}
如果有用,點(diǎn)個贊再走啊~
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者