ShoppingTableViewCell.m 7.04 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//
//  ShoppingTableViewCell.m
//  Lighting
//
//  Created by 曹云霄 on 16/4/29.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "ShoppingTableViewCell.h"

@implementation ShoppingTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
勾芒's avatar
勾芒 committed
16 17
    
    [self uiConfigAction];
曹云霄's avatar
曹云霄 committed
18 19 20
}


勾芒's avatar
勾芒 committed
21 22 23 24 25
#pragma mark -UI
- (void)uiConfigAction
{
    self.ClinchPriceBackView.layer.masksToBounds = YES;
    self.ClinchPriceBackView.layer.cornerRadius = kCornerRadius;
勾芒's avatar
勾芒 committed
26
    self.clinchTextfield.delegate = self;
勾芒's avatar
勾芒 committed
27 28
}

勾芒's avatar
勾芒 committed
29 30 31 32
#pragma mark -赋值
- (void)setModel:(ShopcarModel *)model
{
    _model = model;
勾芒's avatar
勾芒 committed
33
    self.selectedButton.selected = _model.isSelected;
勾芒's avatar
勾芒 committed
34 35
    NSArray *images = [_model.goods.pictures componentsSeparatedByString:@","];
    [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:[images firstObject]] placeholderImage:REPLACEIMAGE];
勾芒's avatar
勾芒 committed
36
    self.goodsInformationLabe.text = _model.goods.name;
37
    self.specifications.text = _model.goods.spec;
polo2013's avatar
polo2013 committed
38
    self.goodsCode.text = _model.goods.code;
勾芒's avatar
勾芒 committed
39
    self.tagsPriceLabe.text = [_model.goods.tagPrice stringValue];
勾芒's avatar
勾芒 committed
40
    self.clinchTextfield.text = [NSString stringWithFormat:@"%.2f",[_model.costPrice floatValue]?[_model.costPrice floatValue]:[_model.goods.tagPrice floatValue]];
41
    self.goodsNumbersLabe.text = [NSString stringWithFormat:@"%ld",_model.goodsNum];
勾芒's avatar
勾芒 committed
42 43 44 45
    if ([_model.costPrice integerValue] == 0) {
        _model.costPrice = _model.goods.tagPrice;
    }
    self.productPriceLabe.text = [NSString stringWithFormat:@"¥%.2f",[self.goodsNumbersLabe.text integerValue]*[_model.costPrice floatValue]];;
勾芒's avatar
勾芒 committed
46
}
曹云霄's avatar
曹云霄 committed
47 48 49 50 51 52

#pragma mark -增加或者减少商品
- (IBAction)reduceAndaddButtonClick:(UIButton *)sender {
    
    //sender.tag == 100   减少
    //sender.tag == 101   增加
勾芒's avatar
勾芒 committed
53 54 55 56 57
    NSInteger goodsNumber = [self.goodsNumbersLabe.text integerValue];
    switch (sender.tag) {
        case 100://减少
        {
            if (goodsNumber <= 1) {
勾芒's avatar
勾芒 committed
58 59 60
                if (self.promptStringBlock) {
                    self.promptStringBlock(@"个数不能小于1");
                }
勾芒's avatar
勾芒 committed
61 62 63 64 65 66 67 68
                //不能小于1
                return;
            }
            goodsNumber --;
        }
            break;
        case 101://增加
        {
69
            if (goodsNumber >= [self.model.goods.number integerValue]) {
勾芒's avatar
勾芒 committed
70
                //不能大于库存
勾芒's avatar
勾芒 committed
71 72 73
                if (self.promptStringBlock) {
                    self.promptStringBlock(@"个数不能大于库存");
                }
勾芒's avatar
勾芒 committed
74 75 76 77 78 79 80 81
                return;
            }
            goodsNumber ++;
        }
            break;
        default:
            break;
    }
勾芒's avatar
勾芒 committed
82
    //改变价格
曹云霄's avatar
曹云霄 committed
83
    WS(weakSelf);
84 85
    if ([self.delegate respondsToSelector:@selector(changeGoodsNumber:WithcostPrice:Withcellindex:returnValue:)]) {
        [self.delegate changeGoodsNumber:goodsNumber WithcostPrice:[self.clinchTextfield.text floatValue]Withcellindex:_cellindex returnValue:^(id value) {
曹云霄's avatar
曹云霄 committed
86 87
            if ([value isKindOfClass:[NSDictionary class]]) {
                if ([value[@"code"] isEqualToNumber:@0]) {
88
                  weakSelf.goodsNumbersLabe.text = [NSString stringWithFormat:@"%ld",goodsNumber];
89 90 91
                  weakSelf.productPriceLabe.text = [NSString stringWithFormat:@"¥%.2f",goodsNumber*[self.clinchTextfield.text floatValue]];
                    self.model.costPrice = [NSNumber numberWithFloat:[self.clinchTextfield.text floatValue]];
                    self.model.goodsNum = goodsNumber;
曹云霄's avatar
曹云霄 committed
92 93 94
                }
            }
        }];
勾芒's avatar
勾芒 committed
95
    }
曹云霄's avatar
曹云霄 committed
96 97 98 99 100 101
}


#pragma mark -商品选中
- (IBAction)selectedButtonClick:(UIButton *)sender {
    
勾芒's avatar
勾芒 committed
102 103 104 105
    if (self.returnCellblock) {
        
        self.returnCellblock(_cellindex);
    }
勾芒's avatar
勾芒 committed
106
    sender.selected = !sender.selected;
曹云霄's avatar
曹云霄 committed
107 108 109
}


勾芒's avatar
勾芒 committed
110 111 112
#pragma mark -成交价完成修改
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
113
    Shoppersmanager *user = [Shoppersmanager manager];
114
    if ([textField.text floatValue] < [user.Shoppers.lowestDiscount floatValue]/100 * [self.model.goods.tagPrice floatValue])
115 116 117 118 119 120
    {
        if (self.promptStringBlock) {
            self.promptStringBlock(@"当前价格低于你的权限范围!");
        }
    }
    else
勾芒's avatar
勾芒 committed
121 122
    {
        //改变价格
勾芒's avatar
勾芒 committed
123
        self.productPriceLabe.text = [NSString stringWithFormat:@"¥%.2f",[self.goodsNumbersLabe.text integerValue]*[textField.text floatValue]];
勾芒's avatar
勾芒 committed
124
        
125 126
        if ([self.delegate respondsToSelector:@selector(changeGoodsNumber:WithcostPrice:Withcellindex:returnValue:)]) {
            [self.delegate changeGoodsNumber:[self.goodsNumbersLabe.text integerValue] WithcostPrice:[self.clinchTextfield.text floatValue]Withcellindex:_cellindex returnValue:^(id value) {
曹云霄's avatar
曹云霄 committed
127
            }];
勾芒's avatar
勾芒 committed
128
        }
勾芒's avatar
勾芒 committed
129 130 131 132
    }
    return YES;
}

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
#pragma mark - <UITextFieldDelegate>
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSScanner      *scanner    = [NSScanner scannerWithString:string];
    NSCharacterSet *numbers;
    NSRange        pointRange = [textField.text rangeOfString:@"."];
    if ( (pointRange.length > 0) && (pointRange.location < range.location  || pointRange.location > range.location + range.length) ){
        numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    }else{
        numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
    }
    if ( [textField.text isEqualToString:@""] && [string isEqualToString:@"."] ){
        return NO;
    }
    short remain = 2; //保留 number位小数
    NSString *tempStr = [textField.text stringByAppendingString:string];
    NSUInteger strlen = [tempStr length];
    if(pointRange.length > 0 && pointRange.location > 0){ //判断输入框内是否含有“.”。
        if([string isEqualToString:@"."]){ //当输入框内已经含有“.”时,如果再输入“.”则被视为无效。
            return NO;
        }
        if(strlen > 0 && (strlen - pointRange.location) > remain+1){ //当输入框内已经含有“.”,当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数,则视当次输入无效。
            return NO;
        }
    }
158 159
    NSRange ZERORange = [textField.text rangeOfString:@"0"];
    if(ZERORange.length == 1 && ZERORange.location == 0){ //判断输入框第一个字符是否为“0”
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        if(![string isEqualToString:@"0"] && ![string isEqualToString:@"."] && [textField.text length] == 1){ //当输入框只有一个字符并且字符为“0”时,再输入不为“0”或者“.”的字符时,则将此输入替换输入框的这唯一字符。
            textField.text = string;
            return NO;
        }else{
            if(pointRange.length == 0 && pointRange.location > 0){ //当输入框第一个字符为“0”时,并且没有“.”字符时,如果当此输入的字符为“0”,则视当此输入无效。
                if([string isEqualToString:@"0"]){
                    return NO;
                }
            }
        }
    }
    NSString *buffer;
    if ( ![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([string length] != 0) ){
        return NO;
    }else{
        return YES;
    }
勾芒's avatar
勾芒 committed
177 178 179
}


曹云霄's avatar
曹云霄 committed
180
@end