// // CartCellTableViewCell.m // IBOSSmini // // Created by guan hong hou on 2017/5/18. // Copyright © 2017年 elongtian. All rights reserved. // #import "CartTableViewCell.h" @implementation CartTableViewCell #pragma mark - 公共函数 /** initWithStyle函数 */ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { } return self; } #pragma mark -委托函数 //textfield回调函数 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *cs; if(textField == _txtSalesQuantity) { cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERSANDDOT] invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; BOOL basicTest = [string isEqualToString:filtered]; if(!basicTest) { return NO; } } return YES; } //textfield结束编辑回调函数 - (void) textFieldDidEndEditing:(UITextField *) field { [self endEditing:YES]; if([self.delegate respondsToSelector:@selector(textValueChange: txtField:)]) { [self.delegate textValueChange:self txtField:field]; } } #pragma mark -私有函数 //初始化购物车cell UI -(void)parseCartInfo:(CartFrame *)cartFrame{ _cartItemModel=cartFrame.cartModel; _cartId=_cartItemModel.cartId; _btnCheck=[UIButton buttonWithType:UIButtonTypeCustom]; _btnCheck.frame=cartFrame.btnCheckF; [_btnCheck addTarget:self action:@selector(btnGoCheck) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:_btnCheck]; _lblCartID=[[UILabel alloc]init]; _lblCartID.frame=CGRectMake(CGRectGetMaxX(cartFrame.btnCheckF)+3, cartFrame.btnCheckF.origin.y, 20, 20); [self.contentView addSubview:_lblCartID]; _lblCartID.hidden=YES; _lblCartID.text=_cartItemModel.cartId; _lblCode=[[UILabel alloc]init]; _lblCode.frame=[cartFrame lblProductCodeF]; _lblCode.text=_cartItemModel.code; _lblCode.font=orderTextFont; [self.contentView addSubview:_lblCode]; _btnDelete=[UIButton buttonWithType:UIButtonTypeCustom]; _btnDelete.frame=cartFrame.btnDeleteF; [self.contentView addSubview:_btnDelete]; [_btnDelete addTarget:self action:@selector(deleteOrder) forControlEvents:UIControlEventTouchUpInside]; UIImageView *deleteImg=[[UIImageView alloc]init]; deleteImg.frame=CGRectMake(0,5,15,15); [deleteImg setImage:[UIImage imageNamed:@"icon_delete"]]; deleteImg.userInteractionEnabled=NO; [_btnDelete addSubview:deleteImg]; UILabel *deleteTitle=[[UILabel alloc]init]; deleteTitle.frame=CGRectMake(CGRectGetMaxX(deleteImg.frame)+3,0,40, 25); deleteTitle.text=@"删除"; deleteTitle.font=orderTextFont; [_btnDelete addSubview:deleteTitle]; UIView *topSeparator=[[UIView alloc]init]; topSeparator.frame=[cartFrame topSeparatorF]; topSeparator.backgroundColor=LineBackgroundColor; [self.contentView addSubview:topSeparator]; UILabel *lblTitleBrand=[[UILabel alloc]init]; NSString *titleBrand=@"品牌:"; lblTitleBrand.text=titleBrand; lblTitleBrand.font=orderTextFont; lblTitleBrand.textColor=LabelGrayTextColor; lblTitleBrand.frame=[cartFrame lblTitleBrandF]; [self.contentView addSubview: lblTitleBrand]; NSString *brand=[_cartItemModel brandName]; if(brand!=nil){ _lblBrandName=[[UILabel alloc]init]; _lblBrandName.text=brand; _lblBrandName.font=orderTextFont; _lblBrandName.frame=[cartFrame lblBrandF]; [self.contentView addSubview:_lblBrandName]; } NSString *titleOnlyCode=@"唯一编码:"; UILabel *lblTitleOnlyCode=[[UILabel alloc]init]; lblTitleOnlyCode.text=titleOnlyCode; lblTitleOnlyCode.font=orderTextFont; lblTitleOnlyCode.textColor=LabelGrayTextColor; lblTitleOnlyCode.frame=[cartFrame lblTitleOnlyCodeF]; [self.contentView addSubview: lblTitleOnlyCode]; NSString *onlyCode=[_cartItemModel onlyCode]; if(onlyCode!=nil){ _lblOnlyCode=[[UILabel alloc]init]; _lblOnlyCode.frame=[cartFrame lblOnlyCodeF]; _lblOnlyCode.font=orderTextFont; _lblOnlyCode.text=onlyCode; [self.contentView addSubview: _lblOnlyCode]; } NSString *titleColorNumber=@"色号:"; UILabel *lblTitleColorNumber=[[UILabel alloc]init]; lblTitleColorNumber.font=orderTextFont; lblTitleColorNumber.text=titleColorNumber; lblTitleColorNumber.textColor=LabelGrayTextColor; lblTitleColorNumber.frame=[cartFrame lblTitleColorNumberF]; [self.contentView addSubview: lblTitleColorNumber]; NSString *colorNumber=[_cartItemModel colorNumber]; if(colorNumber!=nil){ _lblColorNumber=[[UILabel alloc]init]; _lblColorNumber.font=orderTextFont; _lblColorNumber.frame=[cartFrame lblColorNumberF]; _lblColorNumber.text=colorNumber; [self.contentView addSubview:_lblColorNumber]; } NSString *titleSpecification=@"规格:"; UILabel *lblTitleSpecification=[[UILabel alloc]init]; lblTitleSpecification.font=orderTextFont; lblTitleSpecification.textColor=LabelGrayTextColor; lblTitleSpecification.text=titleSpecification; lblTitleSpecification.frame=[cartFrame lblTitleSpecificationF]; [self.contentView addSubview: lblTitleSpecification]; NSString *specification=[_cartItemModel specification]; if(specification!=nil){ _lblSpecification=[[UILabel alloc]init]; _lblSpecification.font=orderTextFont; _lblSpecification.frame=[cartFrame lblSpecificationF]; _lblSpecification.text=specification; [self.contentView addSubview:_lblSpecification]; } NSString *titleGrade=@"等级:"; UILabel *lblTitleGrade=[[UILabel alloc]init]; lblTitleGrade.font=orderTextFont; lblTitleGrade.text=titleGrade; lblTitleGrade.textColor=LabelGrayTextColor; lblTitleGrade.frame=[cartFrame lblTitleGradeF]; [self.contentView addSubview: lblTitleGrade]; NSString *grade=[_cartItemModel gradeName]; if(grade!=nil){ _lblGradeName=[[UILabel alloc]init]; _lblGradeName.font=orderTextFont; _lblGradeName.frame=[cartFrame lblGradeF]; _lblGradeName.text=grade; [self.contentView addSubview:_lblGradeName]; } NSString *titleWareHouse=@"库区:"; UILabel *lblTitleWareHouse=[[UILabel alloc]init]; lblTitleWareHouse.font=orderTextFont; lblTitleWareHouse.text=titleWareHouse; lblTitleWareHouse.textColor=LabelGrayTextColor; lblTitleWareHouse.frame=[cartFrame lblTitleWareHouseAreaF]; [self.contentView addSubview: lblTitleWareHouse]; NSString *warehouse=[_cartItemModel warehouseName]; if(warehouse!=nil){ _lblWarehouseName=[[UILabel alloc]init]; _lblWarehouseName.font=orderTextFont; _lblWarehouseName.frame=[cartFrame lblWareHouseAreaF]; _lblWarehouseName.text=warehouse; [self.contentView addSubview:_lblWarehouseName]; } UIView *middleSeparator=[[UIView alloc]init]; middleSeparator.frame=[cartFrame middleSeparatorF]; middleSeparator.backgroundColor=LineBackgroundColor; [self.contentView addSubview:middleSeparator]; UIButton *substractButton=[UIButton buttonWithType:UIButtonTypeCustom]; [substractButton setTitle:@"-" forState:UIControlStateNormal]; [substractButton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal]; substractButton.frame=cartFrame.btnSubstractQuantityF; substractButton.layer.borderColor=[UIColor blackColor].CGColor; substractButton.layer.borderWidth=1.0; [substractButton addTarget:self action:@selector(substractQuantity) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:substractButton]; _txtSalesQuantity=[UITextField new]; _txtSalesQuantity.frame=cartFrame.txtQuantityF; _txtSalesQuantity.text=_cartItemModel.salesQuantity; _txtSalesQuantity.delegate=self; _txtSalesQuantity.keyboardType=UIKeyboardTypeDecimalPad; _txtSalesQuantity.layer.borderWidth=1.0f; _txtSalesQuantity.tag=1002; _txtSalesQuantity.textAlignment=NSTextAlignmentCenter; _txtSalesQuantity.layer.borderColor=[UIColor blackColor].CGColor; _txtSalesQuantity.font=orderTextFont; [self.contentView addSubview:_txtSalesQuantity]; UIButton *addButton=[UIButton buttonWithType:UIButtonTypeCustom]; [addButton setTitle:@"+" forState:UIControlStateNormal]; [addButton setTitleColor:[UIColor blackColor]forState:UIControlStateNormal]; addButton.frame=cartFrame.btnAddQuantityF; addButton.layer.borderColor=[UIColor blackColor].CGColor; addButton.layer.borderWidth=1.0; [addButton addTarget:self action:@selector(addQuantity) forControlEvents:UIControlEventTouchUpInside]; [self.contentView addSubview:addButton]; UIView *bottomSeparator=[[UIView alloc]init]; bottomSeparator.frame=[cartFrame bottomSeparatorF]; bottomSeparator.backgroundColor=LineBackgroundColor; [self.contentView addSubview:bottomSeparator]; } //销售数量减少函数 -(void)substractQuantity{ NSString *quantity=_txtSalesQuantity.text; if(quantity!=nil){ double quantityValue=quantity.doubleValue; NSInteger circulateType= _cartItemModel.circulateType; if(circulateType==2){ [self.delegate alertMessage:@"请手动输入商品数量"]; return; } --quantityValue; if(quantityValue<0){ quantityValue=0; } NSString *quantity= [NSString stringWithFormat:@"%lld",(long long)quantityValue]; _txtSalesQuantity.text=quantity; if([self.delegate respondsToSelector:@selector(textValueChange: txtField:)]) { [self.delegate textValueChange:self txtField:_txtSalesQuantity]; } } } //删除订单函数 -(void)deleteOrder{ if([self.delegate respondsToSelector:@selector(deleteOrderItem:)]) { [self.delegate deleteOrderItem:_cartItemModel]; } } //销售数量增加函数 -(void)addQuantity{ NSString *quantity=_txtSalesQuantity.text; if(quantity!=nil){ double quantityValue=quantity.doubleValue; NSInteger circulateType= _cartItemModel.circulateType; if(circulateType==2){ [self.delegate alertMessage:@"请手动输入商品数量"]; return; } ++quantityValue; NSString *quantity= [NSString stringWithFormat:@"%lld",(long long)quantityValue]; _txtSalesQuantity.text=quantity; if([self.delegate respondsToSelector:@selector(textValueChange: txtField:)]) { [self.delegate textValueChange:self txtField:_txtSalesQuantity]; } } } //checkbox切换函数 -(void)btnGoCheck { if([self.delegate respondsToSelector:@selector(btnCheckPressed:)]) { [self.delegate btnCheckPressed:self]; } } //设置checkbox背景图片 - (void) setCheckBackground: (BOOL) checked { if(!checked) { _isSelect=NO; [_btnCheck setImage:[UIImage imageNamed:@"order_unchecked"] forState:UIControlStateNormal]; } else { _isSelect=YES; [_btnCheck setImage:[UIImage imageNamed:@"order_checked"] forState:UIControlStateNormal]; } } @end