// // SignUpVC.m // LN_School // // Created by apple on 2017/4/11. // Copyright © 2017年 Danson. All rights reserved. // #import "SignUpVC.h" #import "applyCell.h" @interface SignUpVC () { UITableView *mainTableView; NSMutableArray *titleArray; NSArray *dataArray; NSArray *sheetsArray; NSString *bustype; NSString *headImgPath; } @end @implementation SignUpVC - (void)viewDidLoad { [super viewDidLoad]; [self myInit]; } -(void)myInit { [self setTitle:@"学员报名"]; self.view.backgroundColor = KBackGroundColor; [self goBackByNavigation]; //报名类型 默认为 初领 bustype = @"0"; NSArray *array = @[@[@"培训车型",@"证件类型",@"报名类型",@"培训模式",@"获取照片"],@[@"姓名",@"性别",@"证件号码",@"手机号码",@"地址"],@[@"推荐人"]]; titleArray = [NSMutableArray arrayWithArray:array]; dataArray = @[[NSMutableArray arrayWithArray:@[@"C1 >",@"身份证 >",@"初领 >",@"传统模式 >",@"点击拍照 >"]],[NSMutableArray arrayWithArray:@[@"请输入姓名",@"请输入性别",@"请输入证件号码",@"请输入手机号码",@"请输入地址"]],[NSMutableArray arrayWithArray:@[@"请输入推荐人"]],[NSMutableArray arrayWithArray:@[@"请输入原车型(例如:C1)",@"请输入原驾驶证号",@"请输入初领证日期"]]]; mainTableView = [[UITableView alloc] initWithFrame:kFrame style:UITableViewStyleGrouped]; mainTableView.height -= kNavOffSet; mainTableView.estimatedSectionHeaderHeight = 0; mainTableView.estimatedSectionFooterHeight = 0; [mainTableView setDelegate:self]; [mainTableView setDataSource:self]; [self.view addSubview:mainTableView]; // UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; // mainTableView.tableHeaderView = view; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSize.width, 100)]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 25, kSize.width - 20, 50)]; btn.backgroundColor = defGreen; [btn borderCornorRadios:5]; [btn setTitle:@"提交" textColor:[UIColor whiteColor] font:Font18 fotState:UIControlStateNormal]; [btn target:self Tag:1]; [view addSubview:btn]; mainTableView.tableFooterView = view; } - (void)btnClick:(UIButton *)sender { if (sender.tag == 1) { //提交 [self.view endEditing:YES]; // [mainTableView setEditing:YES]; // [sender becomeFirstResponder]; // if (!myDelegate.isSchool) { // ShowMsg(@"驾校管理员才可以进行此操作!"); // return; // } [self addStudent]; } } #pragma mark 拍照 - (void)takePhoto { //100*120 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { //创建pickerVC UIImagePickerController * pickerVC = [[UIImagePickerController alloc]init]; //设置图片来源类型 pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera; //是否可编辑。 pickerVC.allowsEditing = YES; //设置代理 pickerVC.delegate=self; //前置摄像头 pickerVC.cameraDevice = UIImagePickerControllerCameraDeviceRear; //拍照模式 pickerVC.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; //弹出pickerVC [self presentViewController:pickerVC animated:YES completion:nil]; }else{ ShowMsg(@"相机打开失败"); } } -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) { //找到源图片 //UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; //找到编辑后的图片 UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage]; NSData *data = UIImageJPEGRepresentation([editedImage scaledAndCutToSize:CGSizeMake(100, 120)], 0.5); NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; [self uploadHeadImg:imgString]; } [self dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - tableview delegate -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return titleArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [titleArray[section] count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { applyCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[applyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } [cell.titLabel setText:[titleArray[indexPath.section] objectAtIndex:indexPath.row]]; NSArray *detailArray = dataArray[indexPath.section]; cell.detailField.delegate = self; cell.detailField.tag = indexPath.section * 10 + indexPath.row; if (indexPath.section == 0) { cell.detailField.userInteractionEnabled = NO; cell.detailField.textColor = defGreen; cell.detailField.text = detailArray[indexPath.row]; }else { cell.detailField.userInteractionEnabled = YES; NSArray *array = @[@"请输入姓名",@"请输入性别",@"请输入证件号码",@"请输入手机号码",@"请输入地址",@"请输入推荐人",@"请输入原车型(例如:C1)",@"请输入原驾驶证号",@"请输入初领证日期"]; if ([array containsObject:detailArray[indexPath.row]]) { cell.detailField.text = @""; cell.detailField.placeholder = detailArray[indexPath.row]; }else { cell.detailField.textColor = KTitleColor; cell.detailField.text = detailArray[indexPath.row]; } } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.view endEditing:YES]; if (indexPath.section == 0) { if (indexPath.row == 4) { //拍照 [self takePhoto]; return; } UIActionSheet *myActionSheet; if (indexPath.row == 0) { // sheetsArray = @[@"A1-大型客车", @"A2-牵引车",@"A3-城市公交车",@"B1-中型客车",@"B2-大型货车",@"C1-小型汽车",@"C2-小型自动挡汽车",@"C3-低速载货汽车",@"C4-三轮汽车",@"C5-残疾人专用小型自动挡载客汽车",@"D-普通三轮摩托车",@"E-普通二轮摩托车",@"F-轻便摩托车",@"M-轮式自行机械车",@"N-无轨电车",@"P-有轨电车"]; sheetsArray = [defUser.userDict[@"busiscope"] componentsSeparatedByString:@","]; myActionSheet = [[UIActionSheet alloc] initWithTitle:@"选择培训车型" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil]; for (NSString *title in sheetsArray) { [myActionSheet addButtonWithTitle:title]; } [myActionSheet addButtonWithTitle:@"取消"]; myActionSheet.cancelButtonIndex = myActionSheet.numberOfButtons - 1; }else if (indexPath.row == 1) { sheetsArray = @[@"身份证",@"护照",@"军官证",@"其它"]; myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"身份证",@"护照",@"军官证",@"其它", nil]; }else if (indexPath.row == 2) { sheetsArray = @[@"初领-0",@"增驾-1",@"其它-9"]; myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"初领",@"增驾",@"其它", nil]; }else if (indexPath.row == 3) { sheetsArray = @[@"传统模式",@"先学后付",@"分段式收费"]; myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"传统模式",@"先学后付",@"分段式收费", nil]; } myActionSheet.tag = indexPath.row; [myActionSheet showInView:self.view]; } } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return .1; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 25; } -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { //NSLog(@"1"); [self.view endEditing:YES]; } -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == actionSheet.cancelButtonIndex) { return; } NSMutableArray *array = [dataArray firstObject]; NSString *detailString = sheetsArray[buttonIndex]; if (actionSheet.tag == 2) { //报名类型 detailString = [[sheetsArray[buttonIndex] componentsSeparatedByString:@"-"] firstObject]; bustype = [[sheetsArray[buttonIndex] componentsSeparatedByString:@"-"] lastObject]; if ([bustype isEqualToString:@"1"]) { //mainTableView新增一个section [titleArray addObject:@[@"原车型",@"原驾驶证号",@"初领证日期"]]; [array replaceObjectAtIndex:actionSheet.tag withObject:[NSString stringWithFormat:@"%@ >",detailString]]; [mainTableView reloadData]; return; }else{ if (titleArray.count > 3) { [titleArray removeLastObject]; } } } [array replaceObjectAtIndex:actionSheet.tag withObject:[NSString stringWithFormat:@"%@ >",detailString]]; // NSIndexPath *indexpath = [NSIndexPath indexPathForRow:actionSheet.tag inSection:0]; // [mainTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexpath,nil] withRowAnimation:UITableViewRowAnimationNone]; [mainTableView reloadData];//上面方法二次调用引起崩溃 } #pragma mark textField -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { NSIndexPath *indexpath = indexpath= [NSIndexPath indexPathForRow:textField.tag%10 inSection:textField.tag/10];; //获取当前cell在tableview中的位置 CGRect rectintableview=[mainTableView rectForRowAtIndexPath:indexpath]; [mainTableView setContentOffset:CGPointMake(mainTableView.contentOffset.x,rectintableview.origin.y-25) animated:YES]; return YES; } -(BOOL)textFieldShouldEndEditing:(UITextField *)textField { NSString *textString = textField.text; if (textString.length < 1) { textString = @"请输入"; } NSMutableArray *array = dataArray[textField.tag/10]; [array replaceObjectAtIndex:textField.tag%10 withObject:textString]; [mainTableView reloadData]; return YES; } -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField endEditing:YES]; return YES; } #pragma mark 数据请求 -(void)uploadHeadImg:(NSString *)image { if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:image forKey:@"content"]; [dic setObject:@"" forKey:@"keyword"]; [dic setObject:@"2" forKey:@"type"]; NSString *method = @"upload-2"; [MBProgressHUD showLoadToView:self.view]; [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:self.view]; if (!root) { [self showUPloadImgAlertWithImg:image]; return; } if ([root[@"code"] isEqualToString:@"1"]) { [self showUPloadImgAlertWithImg:image]; return; } //上传头像成功 headImgPath = root[@"body"]; NSMutableArray *array = [dataArray firstObject]; [array replaceObjectAtIndex:4 withObject:@"拍照成功,点击重拍 >"]; NSIndexPath *indexpath = [NSIndexPath indexPathForRow:3 inSection:0]; [mainTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexpath,nil] withRowAnimation:UITableViewRowAnimationNone]; }]; } - (void)showUPloadImgAlertWithImg:(NSString *)image { UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"图片上传失败!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *reGoAction = [UIAlertAction actionWithTitle:@"再试一次" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self uploadHeadImg:image]; }]; [alert addAction:cancelAction]; [alert addAction:reGoAction]; [self presentViewController:alert animated:YES completion:nil]; } -(void)addStudent { if (headImgPath.length < 1) { ShowMsg(@"请采集照片"); headImgPath = @""; return; } NSInteger num = 3; if ([bustype isEqualToString:@"1"]) { num = 4; } NSString *name, *idcard, *sex, *phone, *source, *cardtype, *traintype, *perdritype, *drilicnum, *fstdrilicdate, *address, *trainMode; name=idcard=sex=phone=source=cardtype=traintype=perdritype=drilicnum=fstdrilicdate=address = @""; for (int i = 0; i < num; i ++) { NSMutableArray *array = dataArray[i]; for (int j = 0; j < array.count; j ++) { NSString *string = array[j]; if ([string containsString:@"请输入"] || string.length == 0) { ShowMsg(@"您还有信息未录入"); return; } switch (i) { case 0: if (j == 0) { traintype = [[string componentsSeparatedByString:@" "] firstObject]; }else if (j == 1) { cardtype = [[string componentsSeparatedByString:@" "] firstObject]; //@[@"身份证",@"护照",@"军官证",@"其他"]; if ([cardtype isEqualToString:@"身份证"]) { cardtype = @"1"; }else if ([cardtype isEqualToString:@"护照"]) { cardtype = @"2"; }else if ([cardtype isEqualToString:@"军官证"]) { cardtype = @"3"; }else if ([cardtype isEqualToString:@"其它"]) { cardtype = @"4"; } }else if (j == 3){ trainMode = [[string componentsSeparatedByString:@" "] firstObject]; //培训模式 0 传统模式 1 先学后付 2 分段式收费 if ([trainMode isEqualToString:@"传统模式"]) { trainMode = @"0"; }else if ([trainMode isEqualToString:@"先学后付"]) { trainMode = @"1"; }else if ([trainMode isEqualToString:@"分段式收费"]) { trainMode = @"2"; } } break; case 1: if (j == 0) { name = string; } if (j == 1) { if (![string isEqualToString:@"男"] && ![string isEqualToString:@"女"]) { ShowMsg(@"性别输入错误"); return; } sex = [string isEqualToString:@"男"]?@"1":@"2"; } if (j == 2) { idcard = string; } if (j == 3) { if (![Tools isMobileNumber:string]) { ShowMsg(@"请输入正确的手机号码"); return; } phone = string; } if (j == 4) { address = string; } break; case 2: source = string; break; case 3: if (j == 0) { perdritype = string; } if (j == 1) { drilicnum = string; } if (j == 2) { fstdrilicdate = string; } break; default: break; } } } if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:defUser.userDict[@"id"] forKey:@"userId"]; [dic setObject:name forKey:@"name"]; [dic setObject:idcard forKey:@"idcard"]; [dic setObject:sex forKey:@"sex"]; [dic setObject:phone forKey:@"phone"]; [dic setObject:source forKey:@"source"]; [dic setObject:bustype forKey:@"bustype"]; [dic setObject:traintype forKey:@"traintype"]; [dic setObject:perdritype forKey:@"perdritype"]; [dic setObject:drilicnum forKey:@"drilicnum"]; [dic setObject:fstdrilicdate forKey:@"fstdrilicdate"]; [dic setObject:address forKey:@"address"]; [dic setObject:headImgPath forKey:@"photo"]; [dic setObject:cardtype forKey:@"cardtype"]; [dic setObject:trainMode forKey:@"trainMode"]; NSString *method = @"addStudent"; [MBProgressHUD showLoadToView:self.view]; [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:self.view]; if (!root) { ShowMsg(@"提交失败,请重试"); return; } if ([root[@"code"] isEqualToString:@"1"]) { ShowMsg(root[@"msg"]); return; } ShowMsg(@"报名成功!"); [self.navigationController popViewControllerAnimated:YES]; }]; } #pragma mark - 限制输入字符 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSCharacterSet *cs; //invertedSet方法是去反字符,把所有的除了NUMBERS 里的字符都找出来(包含去空格功能) cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789n"]invertedSet]; // componentsJoinedByString 用于根据一个字符串来将数组连接成一个新的字符串。 NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串 BOOL canChange = [string isEqualToString:filtered]; if (textField.tag == 12 || textField.tag == 13) { return canChange; } return YES; //这样写了以后,输入非数字时不能输入 } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end