SaleInputAddAmountCell.m 2.62 KB
Newer Older
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
//
//  SaleInputAddAmountCell.m
//  HDMall
//
//  Created by Javen on 2017/7/31.
//  Copyright © 2017年 上海勾芒信息科技. All rights reserved.
//

#import "SaleInputAddAmountCell.h"
#import "CalculateHelper.h"
#import "NSString+Additions.h"

@interface SaleInputAddAmountCell ()<UITextFieldDelegate>

@end
@implementation SaleInputAddAmountCell
- (void)awakeFromNib
{
    [super awakeFromNib];
    self.textFieldInput.delegate = self;
    [self.textFieldInput addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
}

/**
 *  设置可编辑状态和不可编辑状态
 */
- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    if (isEdit) {
        self.textFieldInput.userInteractionEnabled = YES;
        self.textFieldInput.textColor = [UIColor blackColor];
    }else{
        self.textFieldInput.userInteractionEnabled = NO;
        self.textFieldInput.textColor = [UIColor lightGrayColor];
    }
}

//实时获取变化之后的数据
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //获取改变之后的字符串
    NSMutableString *futureString = [NSMutableString stringWithString:textField.text];
    [futureString insertString:string atIndex:range.location];
    return [futureString isValidMoneyInput];
}

- (void)textFieldDidChanged:(UITextField *)textField {
    self.model.total = [CalculateHelper decimalNumber:textField.text];
    self.blockNumberChanged();
}


- (void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.textColor = [UIColor blackColor];
    if ([textField.text isEqualToString:@"0.00"]) {
        textField.text = @"";
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    //输入框有文字则显示,避免没输入自动出现0.00的情况
    if (textField.text.length > 0) {
        if ([textField.text isEqualToString:@"0"]) {
            textField.textColor = [UIColor lightGrayColor];
            textField.text = @"0.00";
        }else{
            self.textFieldInput.text = [CalculateHelper getMoneyStringFrom:textField.text];
        }
    }
}

- (void)configCellWithArray:(NSMutableArray *)array indexPath:(NSIndexPath *)indexPath {
    self.model = array[indexPath.row];
    self.textFieldInput.text = self.model.total ? [CalculateHelper getMoneyStringFrom:self.model.total] : nil;
    if ([self.textFieldInput.text isEqualToString:@"0.00"]) {
        self.textFieldInput.textColor = [UIColor lightGrayColor];
    }else{
        self.textFieldInput.textColor = [UIColor blackColor];
    }
    self.labelTitle.text = self.model.payment.name;
}
@end