// // StudentInfoViewController.m // tongxin // // Created by ecell on 2021/11/17. // Copyright © 2021 xTT. All rights reserved. // #import "StudentInfoViewController.h" #import "SelectContactVC.h" #import "EditDeviceHeadTableViewCell.h" #import "EidtDeviceContactWithDeviceTableViewCell.h" #import "NewTableViewCell.h" #import "NewOrdinaryTableViewCell.h" #import "LoginViewController.h" #import "QRcodeViewController.h" #import "MyUILabel.h" #import "StudentInfoModel.h" @interface StudentInfoViewController () { NSInteger _loginType;// 登录的类型 } @property (strong, nonatomic) UITextField *nameTextField; @property (strong, nonatomic) StudentInfoModel *model; @end @implementation StudentInfoViewController @synthesize myDataSource = _myDataSource; - (NSMutableArray *)myDataSource{ if (!_myDataSource) { _myDataSource = [[NSMutableArray alloc] initWithArray:@[@[@"学校",@"班级",@"学号",@"紧急联系人电话"]]]; } return _myDataSource; } -(void)setIsAdd:(BOOL)isAdd{ _isAdd = isAdd; if(isAdd){ _isEdit = YES; } } -(void)setIsShowAllInfo:(BOOL)isShowAllInfo{ _isShowAllInfo = isShowAllInfo; if(isShowAllInfo){ self.myDataSource = [[NSMutableArray alloc] initWithArray:@[@[@"学校",@"班级",@"学号",@"紧急联系人电话"]]]; }else{ self.myDataSource = [[NSMutableArray alloc] initWithArray:@[@[@"学校",@"班级",@"学号",@"紧急联系人电话"]]]; } } -(void)setIsEdit:(BOOL)isEdit{ _isEdit = isEdit; [self.view endEditing:YES]; // 取消所有 文本框焦点 [self.myTableView reloadData]; } - (Device *)editDevice{ _editDevice = cUser.cDevice; return _editDevice; } -(UIBarButtonItem *)addBarButton{ if(!_addBarButton){ _addBarButton = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarItemClick:)]; } return _addBarButton; } - (UIBarButtonItem *)editAndSaveBarButton{ if(!_editAndSaveBarButton){ _editAndSaveBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_data_editor_orange"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarItemClick:)]; } return _editAndSaveBarButton; } -(UIBarButtonItem *)codeBarButton{ if(!_codeBarButton){ _codeBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_or_code"] style:UIBarButtonItemStylePlain target:self action:@selector(codeShowAction:)]; } return _codeBarButton; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; _loginType = [myHelper getNowAccoutType:cUser]; WEAKSELF [_model query:^(StudentInfoModel *model) { weakSelf.model = model; [weakSelf.myTableView reloadData]; } failure:^(NSError *error) { }]; } - (void)viewDidLoad { [super viewDidLoad]; _model = [[StudentInfoModel alloc] init]; self.title = @"学生资料"; UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = -15; self.navigationItem.rightBarButtonItems = @[self.codeBarButton,negativeSpacer,self.editAndSaveBarButton]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[myHelper getImageWithName:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)]; [self initTableCellView]; [self.view addSubview:self.myTableView]; } /** 初始化tableViewCell */ -(void)initTableCellView{ //设备头像头部 [self.myTableView registerNib:[UINib nibWithNibName:@"EditDeviceHeadTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"EditDeviceHeadTableViewCellID"]; //关系尾部 [self.myTableView registerNib:[UINib nibWithNibName:@"EidtDeviceContactWithDeviceTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"EidtDeviceContactWithDeviceTableViewCellID"]; //中间的 cell [self.myTableView registerNib:[UINib nibWithNibName:@"NewOrdinaryTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NewOrdinaryTableViewCellID"]; //去线 self.myTableView.separatorStyle = UITableViewCellSelectionStyleNone; self.myTableView.backgroundColor = RGB(248 , 248, 248); MyUILabel *footer = [[MyUILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 60)]; footer.font =[UIFont systemFontOfSize:14]; footer.adjustsFontSizeToFitWidth = YES; footer.numberOfLines = 0; footer.baselineAdjustment = UIBaselineAdjustmentAlignCenters; [footer setTextColor:[UIColor systemGrayColor]]; [footer setText:@"温馨提示:学生资料可以更改学校、班级、学号和紧急联系人信息"]; self.myTableView.tableFooterView = footer; } -(void)codeShowAction:(UIBarButtonItem *)sender{ UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; QRcodeViewController *vc = [sb instantiateViewControllerWithIdentifier:@"QRcodeViewController"]; [self.navigationController pushViewController:vc animated:YES]; } /** 修改,保存 和 添加的方法 */ - (void)rightBarItemClick:(UIBarButtonItem *)sender{ self.editDevice.name = self.nameTextField.text; if(self.isEdit == YES ||([sender.title isEqualToString:@"添加"])){ [self saveAction]; } else if (self.isEdit == NO){ sender.image = [UIImage imageNamed:@"icon_equipment_information_save"]; self.isEdit = YES; } } -(void)saveAction { self.editDevice.my_phone = cUser.phone; WEAKSELF [_model save:_model success:^{ [weakSelf quitThisVC]; } failure:^(NSError *error) { }]; } -(void)goBack:(id)sender{ [self.view endEditing:YES]; if(!self.isAdd && self.isEdit){ WEAKSELF LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:nil message:@"是否保存修改" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { [weakSelf rightBarItemClick:self.editAndSaveBarButton]; } cancelHandler:^(LGAlertView *alertView) { [weakSelf quitThisVC]; } destructiveHandler:^(LGAlertView *alertView) { }]; [alertView showAnimated:YES completionHandler:nil]; }else{ [self quitThisVC]; } } -(void)quitThisVC{ [self.navigationController popViewControllerAnimated:YES]; } /** 显示完善资料的切换 */ -(void)perfectInformationAction{ self.isShowAllInfo = YES; [self.myTableView reloadData]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *title = self.myDataSource[indexPath.section][indexPath.row]; if (!self.isShowAllInfo && [title isEqualToString:@"完善设备资料"]){ return 0; } return 60; // 50--》60 } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *title = self.myDataSource[indexPath.section][indexPath.row]; // if(indexPath.section == 0 && indexPath.row == 0){ //头部信息 // EditDeviceHeadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditDeviceHeadTableViewCellID" forIndexPath:indexPath]; // [cell setDevice:self.editDevice WithStatus:self.isAdd isEdting:self.isEdit]; // return cell; // } else { NewOrdinaryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewOrdinaryTableViewCellID" forIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryNone; cell.aTextField.textAlignment = NSTextAlignmentRight; cell.aTitleLabel.text = title; cell.aTextField.textColor = [UIColor blackColor]; //在区域的尾部 隐藏线 NSInteger lastIndex = [self.myDataSource[indexPath.section] count] -1; if(lastIndex < 0){ lastIndex = 0; } if(indexPath.section == 0 && indexPath.row == lastIndex){ cell.lineView.hidden = YES; }else if (indexPath.section == 1 && indexPath.row == lastIndex){ cell.lineView.hidden = YES; }else{ cell.lineView.hidden = NO; } // 需要设置一下默认 设置 ,防止 重用 cell input 错乱 [cell defaultInputView]; if(self.isEdit && ([self.editDevice.identity isEqualToString:@"admin"] || _loginType == 3)){ //需 管理员关系 或者是imei游客登录 且 在编辑状态下 才能修改 cell.userInteractionEnabled = YES; } else { if(self.isEdit){ //成员编辑状态下 /* 成员可编辑的状态 @"学校", @"班级", @"学号", @"紧急联系人电话" */ if([title isEqualToString:@"学校"] || [title isEqualToString:@"班级"] || [title isEqualToString:@"学号"] || [title isEqualToString:@"紧急联系人电话"]){ cell.userInteractionEnabled = YES; }else { cell.aTextField.textColor = [UIColor lightGrayColor]; cell.userInteractionEnabled = NO; } }else{ cell.aTextField.textColor = [UIColor lightGrayColor]; cell.userInteractionEnabled = NO; } } if ([title isEqualToString:@"名称"]) { cell.aTextField.placeholder = @"请输入名称"; cell.aTextField.text = self.editDevice.name; self.nameTextField = cell.aTextField; cell.aTextField.delegate = self; //不实现回调 cell.block = nil; return cell; } if ([title isEqualToString:@"性别"]) { cell.aTextField.placeholder = @"选择性别"; NSInteger index = ([self.editDevice.sex isEqualToString: @"girl"]) ? -1 : 0; [cell numberInputViewWithStartNum:-1 EndNum:1 WithIndex:index]; cell.aTextField.text = ([self.editDevice.sex isEqualToString: @"girl"]) ? @"女孩" : @"男孩"; } if ([title isEqualToString:@"学校"]) { cell.aTextField.keyboardType = UIKeyboardTypeDefault; cell.aTextField.placeholder = @"请输入学校名称"; cell.aTextField.text = (_model.schoolName) ?:@""; cell.aTextField.delegate = self; } if ([title isEqualToString:@"班级"]) { cell.aTextField.keyboardType = UIKeyboardTypeDefault; cell.aTextField.placeholder = @"请输入班级"; cell.aTextField.text = (_model.className) ?:@""; cell.aTextField.delegate = self; } if ([title isEqualToString:@"学号"]) { cell.aTextField.keyboardType = UIKeyboardTypeDefault; cell.aTextField.placeholder = @"请输入学号"; cell.aTextField.text = (_model.sno) ?:@""; cell.aTextField.delegate = self; } if ([title isEqualToString:@"紧急联系人电话"]) { cell.aTextField.keyboardType = UIKeyboardTypePhonePad; cell.aTextField.placeholder = @"亲输入紧急联系人电话"; cell.aTextField.text = (_model.contactPhone) ?:@""; cell.aTextField.delegate = self; } WEAKSELF cell.block = ^(NSString *text, NewTableViewCell *blockCell){ blockCell.aTextField.text = text; if ([title isEqualToString:@"性别"]) { weakSelf.editDevice.sex = ([text isEqualToString:@"0"]) ? @"girl" : @"boy"; blockCell.aTextField.text = ([text isEqualToString:@"0"]) ? @"女孩" : @"男孩"; //需要刷新头部信息 [weakSelf.myTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; }else if ([title isEqualToString:@"学校"]) { weakSelf.model.schoolName = text; }else if([title isEqualToString:@"班级"]){ weakSelf.model.className = text; }else if([title isEqualToString:@"学号"]){ weakSelf.model.sno = text; }else if ([title isEqualToString:@"紧急联系人电话"]) { weakSelf.model.contactPhone = text; } }; return cell; // } //return nil; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return [super tableView:tableView heightForHeaderInSection:section]; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ if (section == self.myDataSource.count-1) { //最后一个 if(self.isAdd){ // 是添加状态 if(!self.isShowAllInfo){ //且 未显示完整状态 return 50; } }else{ return 0.01; } } return 0.01; } //MARK: UITextFieldDelegate 限制字数 和输入的 字符 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ // 实时输入数据 NSString *resultStr = [textField.text stringByReplacingCharactersInRange:range withString:string]; NSLog(@"1=%@,2=%@", resultStr, string); if([self.nameTextField isEqual:textField]){ if(resultStr.length > 8){ return NO; }else{ self.editDevice.name = resultStr; return YES; } } if([@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" containsString:string]){ if(resultStr.length > 15){ return NO; }else{ return YES; } } else if([textField.placeholder isEqual:@"请输入学校名称"] || [textField.placeholder isEqual:@"班级"]) { if ([self isInputRuleAndBlank:string] || [string isEqualToString:@""]) { return YES; } if (resultStr.length >30) { return NO; } return YES; } else { if([string isEqualToString:@""]){ return YES; } return NO; } } /** * 字母、数字、中文正则判断(不包括空格) */ - (BOOL)isInputRuleNotBlank:(NSString *)str { NSString *pattern = @"^[a-zA-Z\u4E00-\u9FA5\\d\\(\\)]*$"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:str]; return isMatch; } /** * 字母、数字、中文正则判断(包括空格)【注意3】 */ - (BOOL)isInputRuleAndBlank:(NSString *)str { NSString *pattern = @"^[a-zA-Z\u4E00-\u9FA5\\d\\s]*$"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:str]; return isMatch; } @end