KWMRuleView.m 5.07 KB
//
//  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