// // StuRecordVC.m // jiaPeiC // // Created by apple on 16/8/30. // Copyright © 2016年 JCZ. All rights reserved. // #import "StuRecordVC.h" #import "HolderView.h" #import "DateView.h" #import "MyGradeVC.h" @interface StuRecordVC () { UITableView *_contactTable; HolderView *holderV; UIBarButtonItem *item; UIView *backView; UITextField *idcardTF,*jxbhTF; UIButton *scoreBtn,*countBtn,*beginDateBtn,*endDateBtn,*kemuBtn; UIPickerView *picker; UIView *bgView; //搜索条件 NSArray *pickDataArray; NSInteger whichPicker; NSString *scoreString; NSString *countString; NSString *beginDate; NSString *endDate; NSString *kemuString; NSString *idcardString; NSString *jxbhString; //添加索引加的参数 UISearchController *_searchController; NSMutableArray *_tableSourceArray; NSMutableArray *_letterArray; NSMutableArray *_resultArray; NSMutableArray *models; NSMutableDictionary *groupDic; } @end @implementation StuRecordVC - (void)viewDidLoad { [super viewDidLoad]; self.title = @"学员预考成绩"; //self.view.backgroundColor = backGroundColor; //[self configNavBar]; _tableSourceArray = [[NSMutableArray alloc] init]; _letterArray = [[NSMutableArray alloc] init]; _resultArray = [[NSMutableArray alloc] init]; groupDic = [[NSMutableDictionary alloc] init]; models = [[NSMutableArray alloc] init]; pickDataArray = @[@[@"90",@"91",@"92",@"93",@"94",@"95",@"96",@"97",@"98",@"99",@"100"],@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"]]; scoreString = @"90"; countString = @"1"; beginDate = @""; endDate = @""; kemuString = @"1";//科一和科四 idcardString = @""; jxbhString = @""; //获取26个大写字母 for (int i = 65; i < 91; i ++) { NSString * letter = [[NSString alloc] initWithBytes:&i length:1 encoding:NSUTF8StringEncoding]; [_letterArray addObject:letter]; } item = [[UIBarButtonItem alloc] initWithTitle:@"条件搜索" style:UIBarButtonItemStyleDone target:self action:@selector(clickToSearch)]; [item setTintColor:defGreen]; [self.navigationItem setRightBarButtonItem:item]; _contactTable = [[UITableView alloc] initWithFrame:kFrame style:UITableViewStylePlain]; _contactTable.delegate = self; _contactTable.dataSource = self; //设置sectionIndex [_contactTable setSectionIndexBackgroundColor:[UIColor clearColor]]; [_contactTable setSectionIndexColor:[UIColor redColor]]; [self.view addSubview:_contactTable]; _searchController =[[UISearchController alloc]initWithSearchResultsController:nil]; [_searchController.searchBar sizeToFit]; _searchController.searchResultsUpdater = self; //这个是在搜索时候将之前的隐藏 包括表 无法点击 适合弹出新的tableviewcontroller 默认yes _searchController.dimsBackgroundDuringPresentation = NO; //这个是搜索时候隐藏导航栏 _searchController.hidesNavigationBarDuringPresentation = NO; _searchController.searchBar.placeholder=@"请输入学员姓名或者电话"; _contactTable.sectionIndexColor = [UIColor blueColor]; _contactTable.tableHeaderView=_searchController.searchBar; holderV = [[HolderView alloc] initWithFrame:_contactTable.frame]; [holderV freshBlock:^{ [self getMyStudentByScore]; }]; [self.view addSubview:holderV]; [self getMyStudentByScore]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.view endEditing:YES]; RemoveHUD(); //这个一定要写 要不然会在退出去下一个页面时候 搜索栏有一定时间的存留 if (_searchController.active) { _searchController.active = NO; if (_searchController.searchBar && _searchController.searchBar.superview) { //NSLog(@"这个也一直执行"); [_searchController.searchBar removeFromSuperview]; } } } #pragma mark maintableview delegate (索引) -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return _letterArray; } //响应点击索引时的委托方法 -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { NSInteger count = 0; //NSLog(@"%@-%d",title,(int)index); NSArray *array = [[groupDic allKeys] sortedArrayUsingSelector:@selector(compare:)]; //为了找到索引中没有字目的前一个字母 也是醉了 if (![array containsObject:title]) { int index = (int)[_letterArray indexOfObject:title]; for (int i = index; i > 0; i --) { if ([array containsObject:_letterArray[i]]) { title = _letterArray[i]; break; } } } for(NSString *character in array) { if([character isEqualToString:title]) { return count; } count ++; } return 0; } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if (tableView == _contactTable) { if (_searchController.searchBar.text.length > 0) { if (_resultArray.count > 0) { NSString *str = [[_resultArray firstObject] substringToIndex:1]; for (NSDictionary *dic in models) { if ([dic[@"XM"] hasPrefix:str]) { return dic[@"PY_FIRST"]; } } } //这个永远不会返回 因为 result为空 就不会有这个 return @"A"; }else{ NSArray * array = [[groupDic allKeys] sortedArrayUsingSelector:@selector(compare:)]; return [array objectAtIndex:section]; } } else { NSString *str = [[_resultArray firstObject] substringToIndex:1]; for (NSDictionary *dic in models) { if ([dic[@"XM"] hasPrefix:str]) { return dic[@"PY_FIRST"]; } } } //这个永远不会返回 因为 result为空 就不会有这个 return @"A"; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { //最外层else 相当于对版本适配了 只有iOS8以下 才会进的方法 if (tableView == _contactTable) { if (_searchController.searchBar.text.length > 0) { if (_resultArray.count > 0) { return 1; }else{ return 0; } } return [[groupDic allKeys] count]; } else { if (_resultArray.count > 0) { return 1; }else{ return 0; } } } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == _contactTable) { //sortedArrayUsingSelector 排序 if (_searchController.searchBar.text.length > 0) { return _resultArray.count; } NSArray * array = [[groupDic allKeys] sortedArrayUsingSelector:@selector(compare:)]; return [[groupDic objectForKey:array[section]] count]; } else { //显示搜索的结果 return _resultArray.count; } } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * Identifier = @"cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:Identifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Identifier]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } NSString *p; if (tableView == _contactTable) { if (_searchController.searchBar.text.length > 0) { p = _resultArray[indexPath.row]; }else{ NSArray * array = [[groupDic allKeys] sortedArrayUsingSelector:@selector(compare:)]; p = [[groupDic objectForKey:[array objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; } } else { p = _resultArray[indexPath.row]; } [cell.textLabel setText:p]; // //如果有休学 用不同颜色标注出来 // if ([p hasSuffix:@"已休学"]) { // NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:p]; // NSRange sufRange = [p rangeOfString:@"已休学"]; // [str addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:sufRange]; // cell.textLabel.attributedText = str; // }else{ // [cell.textLabel setText:p]; // } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *showString; if (tableView == _contactTable) { if (_searchController.searchBar.text.length > 0) { showString = _resultArray[indexPath.row]; }else{ NSArray * array = [[groupDic allKeys] sortedArrayUsingSelector:@selector(compare:)]; showString = [[groupDic objectForKey:[array objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; } }else{ showString = _resultArray[indexPath.row]; } for (NSDictionary *dic in models) { BOOL isHas; if (kSize.width > 350.0){ isHas = [showString hasPrefix:[NSString stringWithFormat:@"%@ %@",dic[@"XM"],dic[@"SJHM"]]]; }else{ isHas = [showString hasPrefix:[NSString stringWithFormat:@"%@ %@",dic[@"XM"],dic[@"SJHM"]]]; } if (isHas) { //从这个地方去学员成绩 MyGradeVC *gradeVC = [[MyGradeVC alloc] init]; gradeVC.kmString = kemuString; gradeVC.identifyNum = dic[@"USERID"]; gradeVC.stuName = dic[@"XM"]; [self.navigationController pushViewController:gradeVC animated:YES]; return; } } } #pragma mark UISearchDisplayController 代理方法 - (void)updateSearchResultsForSearchController:(UISearchController *)searchController { //[self searchBarTextDidBeginEditing:_searchController.searchBar]; NSString *searchString = _searchController.searchBar.text; //NSLog(@"searchString-->%@",searchString); [_resultArray removeAllObjects]; for (NSString * name in _tableSourceArray) { if ([name rangeOfString:searchString].location != NSNotFound) { [_resultArray addObject:name]; } } //NSLog(@"_resultArray-->%@",_resultArray); [_contactTable reloadData]; } #pragma mark pickerView -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } //每列对应多少行 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { //1.获取当前的列 NSArray *arayM = pickDataArray[whichPicker]; //2.返回当前列对应的行数 return arayM.count; } //每列每行对应显示的数据是什么 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { //1.获取当前的列 NSArray *arayM = pickDataArray[whichPicker]; //2.获取当前列对应的行的数据 return arayM[row]; } -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return 40; } //选中 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { //获取对应列,对应行的数据 NSString *exceptValue = pickDataArray[whichPicker][row]; //赋值 if (whichPicker == 0) { [scoreBtn setTitle:exceptValue forState:UIControlStateNormal]; scoreString = exceptValue; } if (whichPicker == 1) { [countBtn setTitle:exceptValue forState:UIControlStateNormal]; countString = exceptValue; } } #pragma mark --触发事件 -(void)clickToSearch { item.enabled = NO; backView = [[UIView alloc] initWithFrame:kFrame]; backView.backgroundColor = [UIColor colorWithWhite:.1 alpha:.5]; [self.view addSubview:backView]; CGFloat x, y, w, h, bd; x = 20; w = kSize.width - 2*x; h = 460; y = (kSize.height - h)/2.0; UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)]; searchView.backgroundColor = KBackGroundColor; [searchView borderCornorRadios:5]; [backView addSubview:searchView]; x = y = bd = 10; w = searchView.width - 30; h = 40; NSArray *titles = @[@"学员成绩:",@"连续次数:",@"开始时间:",@"结束时间:",@"考试科目:",@"驾校编号:",@"学员证件号:"]; NSString *beginStr = beginDate; if (beginStr.length < 1) { beginStr = @"请选择开始时间"; } NSString *endStr = endDate; if (endStr.length < 1) { endStr = @"请选择结束时间"; } NSString *kemuStr = @"科目一"; if ([kemuString isEqualToString:@"4"]) { kemuStr = @"科目四"; } NSArray *btnTitles = @[scoreString,countString,beginStr,endStr,kemuStr,@"",@""]; NSArray *describes = @[@"分以上",@"次以上"]; NSMutableArray *btns = [NSMutableArray arrayWithCapacity:6]; for (int i = 0; i < 7; i ++) { UIView *backV = [[UIView alloc] initWithFrame:CGRectMake(x, y + i*(h + bd) , w, h)]; [backV borderColor:kLineColor width:2 cornorRadios:5]; [searchView addSubview:backV]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x + bd, y + i*(h + bd) , 100, h)]; [label setText:titles[i] Font:Font16 TextColor:KContentTextColor Alignment:NSTextAlignmentLeft]; [searchView addSubview:label]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x + bd + 110, y + i*(h + bd) , w - x - bd - 110, h)]; [button setTitle:btnTitles[i] textColor:KContentTextColor font:Font16 fotState:UIControlStateNormal]; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [button target:self Tag:i + 1]; [searchView addSubview:button]; [btns addObject:button]; if (i < 2) { [button addViewWithRect:CGRectMake(x + bd + 100, y + i*(h + bd) + 30 , 35, 1.5) Color:KContentTextColor]; label = [[UILabel alloc] initWithFrame:CGRectMake(x + bd + 150, y + i*(h + bd) , w - x - bd - 150, h)]; [label setText:describes[i] Font:Font16 TextColor:KContentTextColor Alignment:NSTextAlignmentLeft]; [searchView addSubview:label]; }else if (i == 5){ [button removeFromSuperview]; jxbhTF = [[UITextField alloc]initWithFrame:CGRectMake(x + bd + 110, y + i*(h + bd) , w - x - bd - 110, h)]; jxbhTF.placeholder = @"请输入驾校编号"; jxbhTF.delegate = self; [searchView addSubview:jxbhTF]; }else if (i == 6){ [button removeFromSuperview]; idcardTF = [[UITextField alloc]initWithFrame:CGRectMake(x + bd + 110, y + i*(h + bd) , w - x - bd - 110, h)]; idcardTF.placeholder = @"请输入学员证件号"; idcardTF.delegate = self; [searchView addSubview:idcardTF]; }else{ button.x -= 10; button.width += 10; } } scoreBtn = btns[0]; countBtn = btns[1]; beginDateBtn = btns[2]; endDateBtn = btns[3]; kemuBtn = btns[4]; y += 6*(h + bd); w = (w - 2*bd)/2.0; NSArray *titleArrays = @[@"取消",@"查询"]; for (int i = 0; i < 2; i ++) { UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x + i*(w + 2*bd), y , w, h)]; [button setTitle:titleArrays[i] textColor:defGreen font:Font16 fotState:UIControlStateNormal]; [button borderColor:kLineColor width:2 cornorRadios:5]; [button target:self Tag:i + 7];//7 8 [searchView addSubview:button]; } } -(void)btnClick:(UIButton *)sender { if (bgView && sender.tag < 4) { ShowMsg(@"请先选择分数或者连续次数"); return; } switch (sender.tag) { case 1: { whichPicker = 0; bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kSize.height - 180, kSize.width, 180)]; bgView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:bgView]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(kSize.width/2.0 - 20, 0, kSize.width/2.0, 35)]; [btn setTitle:@"完成" textColor:defGreen font:Font18 fotState:UIControlStateNormal]; btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; [btn target:self Tag:9]; [bgView addSubview:btn]; picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 30, kSize.width, 150)]; picker.delegate = self; picker.dataSource = self; [bgView addSubview:picker]; [btn addViewWithRect:CGRectMake(0, 35, kSize.width, 1.5) Color:KContentTextColor]; } break; case 2: { whichPicker = 1; bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kSize.height - 180, kSize.width, 180)]; bgView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:bgView]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(kSize.width/2.0 - 20, 0, kSize.width/2.0, 35)]; [btn setTitle:@"完成" textColor:defGreen font:Font18 fotState:UIControlStateNormal]; btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; [btn target:self Tag:9]; [bgView addSubview:btn]; picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 30, kSize.width, 150)]; picker.delegate = self; picker.dataSource = self; [bgView addSubview:picker]; [btn addViewWithRect:CGRectMake(0, 35, kSize.width, 1.5) Color:KContentTextColor]; } break; case 3: { DateView *dateV = [[DateView alloc] init]; [dateV setStyle:0]; [dateV showWithComplete:^(NSString * result) { beginDate = result; [beginDateBtn setTitle:beginDate forState:UIControlStateNormal]; }]; } break; case 4: { DateView *dateV = [[DateView alloc] init]; [dateV setStyle:0]; [dateV showWithComplete:^(NSString * result) { endDate = result; [endDateBtn setTitle:endDate forState:UIControlStateNormal]; }]; } break; case 5: { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"请选择科目" delegate:self cancelButtonTitle:@"科目一" otherButtonTitles:@"科目四", nil]; alert.tag = 2; [alert show]; } break; case 7: { if (backView) { //取消的时候 如果picker还在 要移除了 if (bgView) { [bgView removeFromSuperview]; bgView = nil; } [backView removeFromSuperview]; } item.enabled = YES; } break; case 8: { if (backView) { //取消的时候 如果picker还在 要移除了 if (bgView) { [bgView removeFromSuperview]; bgView = nil; } [backView removeFromSuperview]; } item.enabled = YES; [self getMyStudentByScore]; } break; case 9://whichPicker 完成 { if (bgView) { [bgView removeFromSuperview]; bgView = nil; } } break; default: break; } } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == 2){ if (buttonIndex == 0) { //取消 科目一 kemuString = @"1"; [kemuBtn setTitle:@"科目一" forState:UIControlStateNormal]; }else if (buttonIndex == 1){ kemuString = @"4"; [kemuBtn setTitle:@"科目四" forState:UIControlStateNormal]; } } } #pragma mark 数据请求 /** * 根据连续次数和成绩查询满足的学员 * @param String score 满足考试分数 * @param String count 连续次数 * @param String isYk 是否已预考 * @param String zjhm 教练证件号码 * */ //String getMyStudentByScore(String serviceName, String key,String score,String count,String isYk,String zjhm); //考试成绩 - (void)getMyStudentByScore { if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableDictionary * mDic = [NSMutableDictionary new]; [mDic setObject:idcardString forKey:@"idcard"]; [mDic setObject:kemuString forKey:@"subject"]; [mDic setObject:scoreString forKey:@"score"]; [mDic setObject:countString forKey:@"count"]; [mDic setObject:beginDate forKey:@"startTime"]; [mDic setObject:endDate forKey:@"endTime"]; [mDic setObject:jxbhString forKey:@"jxbh"]; NSString* method = @"getStuExams"; [NetManager requestAnythingWithURL:method dictionary:mDic dataArray:nil completion:^(NSDictionary *root) { [holderV setHidden:NO]; if (!root) { ShowErrorMsg(@"请求失败!"); return; } if ([root[@"code"] isEqualToString:@"1"]) { ShowErrorMsg(root[@"msg"]); return; } if ([root[@"body"] count] < 1) { ShowMsg(@"已加载全部数据"); return; } [holderV setHidden:YES]; models = root[@"body"]; // [self acceptData]; }]; } -(void)acceptData { _searchController.searchBar.placeholder=[NSString stringWithFormat:@"请输入学员姓名或电话(当前共%d人)",(int)models.count]; [groupDic removeAllObjects]; [_tableSourceArray removeAllObjects]; /** * LLJLMC = 陈恺声, CRDATE = 2016-02-17 10:26:14, ZJHM = 130425198104097711, SJHM = 15272271043, LXDH = 15272271043, XM = 张运奇, CX = C1, PY_FIRST = Z, XB = 男, ID = 776976 */ NSString *keMuString; for (NSDictionary *dic in models) { keMuString = @"科目一"; if ([kemuString isEqualToString:@"4"]) { keMuString = @"科目四"; } NSString *letter = dic[@"PY_FIRST"]; if (![groupDic objectForKey:letter]) { [groupDic setObject:[NSMutableArray array] forKey:letter]; } if (kSize.width > 350.0){ [_tableSourceArray addObject:[NSString stringWithFormat:@"%@ %@ %@",dic[@"XM"],dic[@"SJHM"],keMuString]]; [[groupDic objectForKey:letter] addObject:[NSString stringWithFormat:@"%@ %@ %@",dic[@"XM"],dic[@"SJHM"],keMuString]]; }else{ [_tableSourceArray addObject:[NSString stringWithFormat:@"%@ %@ %@",dic[@"XM"],dic[@"SJHM"],keMuString]]; [[groupDic objectForKey:letter] addObject:[NSString stringWithFormat:@"%@ %@ %@",dic[@"XM"],dic[@"SJHM"],keMuString]]; } } [_contactTable reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end