// // CartItemCell.m // RedAnt ERP Mobile // // Created by Ray on 14-7-9. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved. // #import "ModelItemCell.h" #import "RANetwork.h" #import "EditModelPriceViewController.h" #import "RAUtils.h" #import "BundleModelViewController.h" #import "CartViewController.h" #if defined(BUILD_HOMER) || defined(BUILD_GATIT) #import "MasterModelViewController.h" #endif #define NUMBERS @"0123456789\n" @implementation ModelItemCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } -(void) init_Stepper:(int) step max:(int) max min:(int)min value:(int)value { // [self.stepper becomeFirstResponder]; if(self.stepper!=nil) { if(min<=0) min=1; if(step<=0) step=1; self.stepper.minimumValue= min; self.stepper.stepValue= step; self.stepper.value= value; self.pre_val = value; [self.stepper addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged]; self.totalchange=0; } } - (IBAction)setpperChanged:(UIStepper *)sender { DebugLog(@"setpperChanged"); int val_change=(int)sender.value-self.pre_val; _totalchange=_totalchange+val_change; self.pre_val = (int)sender.value; [self cancel_setQTY]; [self performSelector:@selector(delayRun) withObject:nil afterDelay:0.8]; } -(void) cancel_setQTY { [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayRun) object:nil]; } -(void) delayRun { // NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(calculate) object:nil]; // // 启动 // [thread start]; [RANetwork request_update_cartqty:self.cart_id value:self.pre_val completionHandler:^(NSMutableDictionary *result) { NSDictionary* cart_json =result; self.totalchange=0; if([[cart_json valueForKey:@"result"] intValue]==2) { if(self.onReturnQTY) self.onReturnQTY(self.pre_val,cart_json); // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) { // [self.delegate returnQTY:sender.value ]; // } } else { UIViewController* vc=[RAUtils getViewController :self]; [((CartViewController *)vc).itemListTable reloadData]; // [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ; [RAUtils message_box:@"Change Model Count" message:[cart_json valueForKey:@"err_msg"] completion:nil]; } }]; } - (void)valueChanged:(UIStepper *)sender { self.editCount.text = [NSString stringWithFormat:@"%d",(int)sender.value ]; [self set_Count:(int)sender.value ]; // // [celldelegate stepClicked:(int)sender.value]; // -(void) stepClicked:(int) value; } -(void) set_Count:(int) count { self.stepper.value=count; if(self.enable_longpress) { // [self addGestureRecognizer: [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)]]; } double dprice=0; if(self.bundle_item!=nil) { int citem=0; int bcount=[[self.bundle_item valueForKey:@"count"] intValue]; for(int bc=0;bc }]; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } #pragma mark textField delegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return NO; } -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ DebugLog(@"text:%@",textField.text); NSCharacterSet *cs; cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; BOOL canChange = [string isEqualToString:filtered]; return canChange; } - (void)textFieldDidEndEditing:(UITextField *)textField { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; int qty = [textField.text intValue]; if(qty==0) qty=self.stepper.minimumValue; #ifdef MPACK if ((int)qty % (int)self.stepper.stepValue != 0) { UIViewController* vc=[RAUtils getViewController :self]; [((CartViewController *)vc).itemListTable reloadData]; [RAUtils message_box:@"Change Model Count" message:[NSString stringWithFormat:@"Sold in quantities of %d",(int)(self.stepper.stepValue)] completion:nil]; return; } #endif int c=qty; int m=c%(int)(self.stepper.stepValue); if(m!=0&&appDelegate.alert_sold_in_quantities) { UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Sold in quantities of %d, Are you sure?",(int)(self.stepper.stepValue)] message:nil preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { [self set_Count:qty]; [RANetwork request_update_cartqty:self.cart_id value:qty completionHandler:^(NSMutableDictionary *result) { NSDictionary* cart_json =result; if([[cart_json valueForKey:@"result"] intValue]==2) { // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) { // [self.delegate returnQTY:qty]; // } if(self.onReturnQTY) self.onReturnQTY(qty,cart_json); } else { UIViewController* vc=[RAUtils getViewController :self]; [((CartViewController *)vc).itemListTable reloadData]; // [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ; [RAUtils message_box:@"Change Model Count" message:[cart_json valueForKey:@"err_msg"] completion:nil]; } }]; }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { DebugLog(@"No"); textField.text = [NSString stringWithFormat:@"%d",self.pre_val]; }]; [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) UIViewController *vc= [RAUtils getViewController:self]; [vc presentViewController:alertControl animated:YES completion:nil]; } else { [self set_Count:qty]; [RANetwork request_update_cartqty:self.cart_id value:qty completionHandler:^(NSMutableDictionary *result) { NSDictionary* cart_json =result; if([[cart_json valueForKey:@"result"] intValue]==2) { // if (self.delegate && [self.delegate respondsToSelector:@selector(returnQTY:)]) { // [self.delegate returnQTY:qty]; // } if(self.onReturnQTY) self.onReturnQTY(qty,cart_json); } else { UIViewController* vc=[RAUtils getViewController :self]; [((CartViewController *)vc).itemListTable reloadData]; // [RAUtils message_box:[cart_json valueForKey:@"err_msg"] message:[cart_json valueForKey:@"err_msg"] completion:nil] ; [RAUtils message_box:@"Change Model Count" message:[cart_json valueForKey:@"err_msg"] completion:nil]; } }]; } } - (void)textFieldDidBeginEditing:(UITextField *)textField { NSString* text = textField.text; if(text.length==0) text=@"0"; self.pre_val = [textField.text intValue]; // self.last_edit = textField.text; } @end