1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// KWMRuleView.m
// iCemarose
//
// Created by 陈荣科 on 16/9/7.
// Copyright © 2016年 kollway. All rights reserved.
//
#import "KWMRuleView.h"
#import "Buy/Buy.h"
#import "KWMStringUtil.h"
#import "KWMVariants.h"
@implementation KWMRuleView{
NSInteger rowNum;
}
- (id)init{
if (self = [super init]) {
[self initViewContentiew];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
if(self = [super initWithFrame:frame]){
[self initViewContentiew];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
[self initViewContentiew];
}
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
[self initViewContentiew];
}
- (void)initViewContentiew{
[[NSBundle mainBundle] loadNibNamed:@"KWMRuleView" owner:self options:nil];
[self addSubview:self.vContent];
self.vContent.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
self.lbHeader.tag = 10086;
}
static CGFloat height = 13;
static CGFloat width = 50;
static CGFloat spaceTop = 14;//顶部lb高度
static CGFloat marginTop = 5;//小图标上间距
static CGFloat marginRight = 15;
#define Heigth (self.vContent.frame.size.height)
#define Width (self.vContent.frame.size.width)
//一列多少行
- (void)getRowNum{
rowNum = (Heigth - spaceTop)/(marginTop + height);
}
//第几列
- (NSInteger)getCulNum:(NSInteger)num{
CGFloat cul = num/rowNum;
if (num%rowNum) {
cul += 1;
}
return cul;
}
- (void)addLabel:(BUYProductVariant *)variant And:(NSString *)str num:(NSInteger)num AndCul:(NSInteger)cul{
if (str == nil && variant!=nil) {
str = variant.title;
}
if ([KWMStringUtil isEmpty:str]){
return;
}
num = num==0 ? 8:num;
CGFloat top = spaceTop + (num-1)*(marginTop+height);
// NSLog(@"cul: %ld num: %ld top: %f",(long)cul,(long)num,top);
CGFloat left = Width - (cul - 1)*marginRight - cul*width;
UILabel *lbAge = [[UILabel alloc] initWithFrame:CGRectMake(left, top, width, height)];
lbAge.text = str;
lbAge.font = [UIFont fontWithName:@"PingFang SC" size:9];
// lbAge.layer.borderWidth = 1;
// lbAge.layer.borderColor = [UIColor redColor].CGColor;
lbAge.textAlignment = NSTextAlignmentCenter;
[self.vContent addSubview:lbAge];
}
-(void)setData:(BUYProduct *)product{
if(product == nil || product.variants == nil){
return;
}
//[self.vContent.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
for (UIView *subviews in [self.vContent subviews]) {
if (subviews.tag!=10086) {
[subviews removeFromSuperview];
}
}
NSArray *sizeArray = product.variants.array;
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < sizeArray.count; ++i) {
BUYProductVariant *variant = [sizeArray objectAtIndex:i];
if (variant.availableValue) {
[arr addObject:variant];
}
}
[self setRuleDataSize:arr By:NO];
}
//搜索数据Model赋值
- (void)setDataProduct:(KWMDataProduct *)dataProduct{
// NSDictionary *sizeDict = dataProduct.options.lastObject;
// NSArray *sizeArray = [sizeDict objectForKey:@"values"];
// if(dataProduct == nil || sizeArray.count == 0){
// return;
// }
// NSMutableArray *arr = [NSMutableArray array];
// for (int i = 0; i < sizeArray.count; ++i) {
// BUYProductVariant *variant = [sizeArray objectAtIndex:i];
// if (variant.availableValue) {
// [arr addObject:variant];
// }
// }
if(dataProduct == nil){
return;
}
NSArray *variants = dataProduct.variants;
NSMutableArray *sizeArray = [NSMutableArray array];
if(variants != nil){
for(KWMVariants *variant in variants ){
if (variant.inventoryQuantity != nil && variant.inventoryQuantity.integerValue>0) {
[sizeArray addObject:variant.title];
}
}
}
if(sizeArray.count == 0){
[sizeArray addObject:@""];
}
[self setRuleDataSize:sizeArray By:YES];
}
- (void)setRuleDataSize:(NSArray *)sizeArray By:(BOOL)isDataProduct{
for (UIView *subviews in [self.vContent subviews]) {
if (subviews.tag!=10086) {
[subviews removeFromSuperview];
}
}
// NSArray *sizeArray = product.variants.array;
[self getRowNum];
CGFloat cul = [self getCulNum:sizeArray.count];
CGFloat contenWidth = cul*width + (cul-1)*marginRight;
if (cul == 1) {
contenWidth = 50;
}
self.vContent.frame = CGRectMake(self.frame.size.width - contenWidth, 0, contenWidth+5, self.frame.size.height);
for (int i = 0; i < sizeArray.count; ++i) {
NSInteger temp = i+1;
cul = [self getCulNum:temp];
CGFloat num = temp>rowNum ? temp%rowNum:temp;
if (isDataProduct) {
[self addLabel:nil And:[sizeArray objectAtIndex:i] num:num AndCul:cul];
}else{
[self addLabel:[sizeArray objectAtIndex:i] And:nil num:num AndCul:cul];
}
}
}
@end