// // SelectSchoolVC.m // jiaPei // // Created by apple on 2017/1/10. // Copyright © 2017年 JCZ. All rights reserved. #import "SelectSchoolVC.h" #import "HolderView.h" #import "Tools.h" #import "AllCoachPlanVC.h" #import "SchCell.h" #import "FenceSiteVC.h" #import #import //引入定位功能所有的头文件(新) @interface SelectSchoolVC () { HolderView *holderV; //搜索栏 UITableView *_contactTable; UISearchBar *_searchBar; UISearchController *_searchController; UIBarButtonItem *item; //索引 NSMutableArray *_letterArray; NSMutableArray *_resultArray; NSMutableArray *models; NSMutableDictionary *groupDic; NSString *dqbh, *qxbh; NSString *pickDQBH,*pickQXBH; //地区选择栏 UIPickerView *picker; NSInteger currentSlect; UIView *bgView; NSArray *areaArray; NSArray *pickDataArray; //地图定位 CLLocationCoordinate2D myLocation; } @end @implementation SelectSchoolVC - (void)viewDidLoad { [super viewDidLoad]; [self setTitle:@"选择驾校"]; if ([_skipType isEqualToString:@"1"]) { [self configNavigationBar]; self.navigationController.navigationBar.translucent = NO; }else{//2 3 [self configNavigationBar]; } dqbh = defUser.userDict[@"city"]; qxbh = defUser.userDict[@"country"]; //定位 [[MapManager sharedManager] updateLocationWithCompleteBlock:^(BOOL success, CLLocation * _Nonnull location) { if (success) { //储存自己当前位置 self->myLocation = location.coordinate; [self getSchList]; }else { [self getSchList]; } }]; _letterArray = [[NSMutableArray alloc] init]; _resultArray = [[NSMutableArray alloc] init]; groupDic = [[NSMutableDictionary alloc] init]; models = [[NSMutableArray alloc] init]; [self loadArea]; //获取26个大写字母 for (int i = 65; i < 91; i ++) { NSString * letter = [[NSString alloc] initWithBytes:&i length:1 encoding:NSUTF8StringEncoding]; [_letterArray addObject:letter]; } //创建一个表 _contactTable = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain]; _contactTable.height -= kNavOffSet; [_contactTable setDataSource:self]; [_contactTable setDelegate:self]; _contactTable.rowHeight = 100; _contactTable.tableFooterView = [UIView new]; //设置sectionIndex [_contactTable setSectionIndexBackgroundColor:[UIColor clearColor]]; [_contactTable setSectionIndexColor:[UIColor redColor]]; [self.view addSubview:_contactTable]; //切换城市 item = [[UIBarButtonItem alloc] initWithTitle:@"切换城市" style:UIBarButtonItemStyleDone target:self action:@selector(clickToChangeCity)]; [item setTintColor:defGreen]; [self.navigationItem setRightBarButtonItem:item]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { _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; //[self searchBarTextDidBeginEditing:_searchController.searchBar]; //不用弹出 因为没有单独的tableviewcontroller //[self presentViewController:_searchController animated:YES completion:nil]; } else { //实例化一个搜索框 _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, kSize.width, 40)]; //设置背景色 [_searchBar setBarTintColor:[UIColor grayColor]]; //设置搜索框风格 [_searchBar setSearchBarStyle:UISearchBarStyleProminent]; [_searchBar setPlaceholder:@"请输入驾校名称"]; //设置显示取消按钮 [_searchBar setShowsCancelButton:YES animated:YES]; //改变索引的颜色 _contactTable.sectionIndexColor = [UIColor blueColor]; //改变索引选中的背景颜色 //设置表的表头 [_contactTable setTableHeaderView:_searchBar]; //搜索显示器 Display显示 // UISearchDisplayController * displayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self]; // [displayController setDelegate:self]; // [displayController setSearchResultsDataSource:self]; // [displayController setSearchResultsDelegate:self]; } holderV = [[HolderView alloc] initWithFrame:_contactTable.frame]; [holderV freshBlock:^{ [self getSchList]; }]; [self.view addSubview:holderV]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.view endEditing:YES]; //这个一定要写 要不然会在退出去下一个页面时候 搜索栏有一定时间的存留 if (_searchController.active) { _searchController.active = NO; if (_searchController.searchBar && _searchController.searchBar.superview) { //NSLog(@"这个也一直执行"); [_searchController.searchBar removeFromSuperview]; } } } - (void)btnClick:(UIButton *)sender { if (sender.tag == 1) { if (bgView) { [bgView removeFromSuperview]; bgView = nil; item.title = @"切换城市"; dqbh = pickDQBH; qxbh = pickQXBH; } [self getSchList]; } } - (void)clickToChangeCity { if (bgView) { [bgView removeFromSuperview]; bgView = nil; item.title = @"切换城市"; return; } item.title = @"取消"; currentSlect = 0; //沈阳市 福州市 pickDQBH = @"2101"; pickQXBH = @""; bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kSize.height - 295 - kNavOffSet-kSafeAreaBottomHeight, kSize.width, 295+kSafeAreaBottomHeight)]; bgView.backgroundColor = backGroundColor; [self.view addSubview:bgView]; // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(kSize.width/2.0, 0, kSize.width/2.0 - 20, 40)]; // [label setText:@"完成" Font:Font17 TextColor:defGreen Alignment:NSTextAlignmentRight]; // [bgView addSubview:label]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(kSize.width-100, 5, 80, 40)]; [btn setTitle:@"完成" forState:UIControlStateNormal]; [btn setTitleColor:defGreen forState:UIControlStateNormal]; btn.layer.cornerRadius = 5; btn.layer.masksToBounds = YES; btn.layer.borderWidth = .5; btn.layer.borderColor = defGreen.CGColor; [btn target:self tag:1]; [bgView addSubview:btn]; picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, kSize.width, 245)]; picker.delegate = self; picker.dataSource = self; [bgView addSubview:picker]; [btn addViewWithRect:CGRectMake(0, 50, kSize.width, 1) Color:contentTextColor]; } - (void)finishBlock:(BlockTypeVo)block { finishBlock = block; } #pragma mark 获取地区信息 -(void)loadArea { NSMutableArray *firArr,*secArr; firArr = [NSMutableArray array];//市name【x市】 secArr = [NSMutableArray array];//区name areaArray = [DB_Que_Helper queryArea]; for (NSDictionary *dic in areaArray) { if ([dic[@"parent_code"] isEqualToString:@"2100"]) { [firArr addObject:dic[@"name"]]; [secArr addObject:[NSMutableArray arrayWithArray:@[@"全市"]]]; }else{ // secArr[firArr.count-1] 它们是有序的 [secArr[firArr.count-1] addObject:dic[@"name"]]; } } pickDataArray = @[firArr,secArr]; } #pragma mark pickerView -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return pickDataArray.count; } //每列对应多少行 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component == 0) { return [[pickDataArray firstObject] count]; }else{ NSInteger index = currentSlect; NSArray *array = [pickDataArray lastObject]; return [array[index] count]; } } //每列每行对应显示的数据 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { NSArray *array; if (component == 0) { array = [pickDataArray firstObject]; }else{ NSInteger index = currentSlect; array = [pickDataArray lastObject][index]; } return array[row]; } -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return 40; } //选中 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component == 0) { currentSlect = row; //重新加载第二列的数据 [pickerView reloadComponent:1]; //让第二列归位 [pickerView selectRow:0 inComponent:1 animated:YES]; for (NSDictionary *dic in areaArray) { if ([dic[@"name"] isEqualToString:[[pickDataArray firstObject] objectAtIndex:row]]) { pickDQBH = dic[@"code"]; break; } } }else{ NSInteger index = currentSlect; NSArray *array = [pickDataArray lastObject][index]; for (NSDictionary *dic in areaArray) { if ([array[row] isEqualToString:@"全市"]) { pickDQBH = @""; break; }else if ([dic[@"name"] isEqualToString:array[row]]) { pickQXBH = dic[@"code"]; if (pickQXBH.length < 6) { pickQXBH = @""; } break; } } } } #pragma mark 设置tableView右边的索引 -(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[@"STU_NAME"] 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[@"STU_NAME"] 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 { SchCell *cell = [SchCell cellWithTableView:tableView]; 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]; for (NSDictionary *dic in models) { if ([dic[@"SHORTNAME"] isEqualToString:p]) { cell.selectDic = dic; } } 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) { if ([dic[@"SHORTNAME"] isEqualToString:showString]) { if (![_skipType isEqualToString:@"2"]) { //调绑定接口 [self bindSchoolWithJXBH:dic[@"ID"]]; }else{ //跳转选择训练场地或者选择教练页面 [self getSchPlacesWithDic:dic]; break; } } } } #pragma mark UISearchDisplayController 代理方法 ////iOS7搜索的时候 会调用这个方法 //- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString //{ // //NSLog(@"searchString====%@",searchString); // [_resultArray removeAllObjects]; // // //按名字查找 // for (NSDictionary *dic in models) // { // if ([dic[@"SHORTNAME"] rangeOfString:searchString].location != NSNotFound) // { // [_resultArray addObject:dic[@"SHORTNAME"]]; // } // } // // [_contactTable reloadData]; // return YES; //} ////点击不同的选择范围的时候调用 //-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption //{ // [self searchDisplayController:controller shouldReloadTableForSearchString:_searchBar.text]; // return YES; //} //iOS8搜索调用的方法 - (void)updateSearchResultsForSearchController:(UISearchController *)searchController { //[self searchBarTextDidBeginEditing:_searchController.searchBar]; NSString *searchString = _searchController.searchBar.text; //NSLog(@"searchString-->%@",searchString); [_resultArray removeAllObjects]; //按名字查找 for (NSDictionary *dic in models) { if ([dic[@"SHORTNAME"] rangeOfString:searchString].location != NSNotFound) { [_resultArray addObject:dic[@"SHORTNAME"]]; } } //NSLog(@"_resultArray-->%@",_resultArray); [_contactTable reloadData]; } #pragma mark 数据请求 - (void)bindSchoolWithJXBH:(NSString *)jxbh { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"userId" Value:defUser.userDict[@"id"]]; [arr addPro:@"schId" Value:jxbh]; NSString* method = @"bindSchool"; [MBProgressHUD showLoadToView:self.view]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { [MBProgressHUD hideHUDForView:self.view]; //NSLog(@"绑定驾校---->%@,,,,,%@",arr,root); if (!root) { ShowMsgFailed(); return; } [LoadingView showMsg:root[@"body"]]; if ([root[@"code"] isEqualToString:@"0"]) { //绑定成功 [LoginViewController reLoginFromViewController:self finishBlock:^{ if (finishBlock) { finishBlock(); } }]; [self dismissViewControllerAnimated:YES completion:nil]; } }]; } - (void)getSchList { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"dqbh" Value:dqbh]; [arr addPro:@"qxbh" Value:qxbh]; [arr addPro:@"lat" Value:[NSString stringWithFormat:@"%f",myLocation.latitude]]; [arr addPro:@"lng" Value:[NSString stringWithFormat:@"%f",myLocation.longitude]]; NSString* method = @"getSchList";//经纬度只为了返回jui [MBProgressHUD showLoadToView:self.view]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { [MBProgressHUD hideHUDForView:self.view]; //NSLog(@"驾校列表---->%@,,,,,%@",arr,root); [holderV setHidden:NO]; if (!root) { [LoadingView showMsg:@"查询失败"]; return; } NSString* code = root[@"code"]; if (code.intValue > 0) { [LoadingView showMsg:root[@"body"]]; return; } if ([root[@"body"] count] < 1) { return; } models = root[@"body"]; [self acceptData]; [holderV setHidden:YES]; }]; } -(void)acceptData { if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { _searchController.searchBar.placeholder=[NSString stringWithFormat:@"请输入驾校名称(当前共%d家)",(int)models.count]; }else{ [_searchBar setPlaceholder:[NSString stringWithFormat:@"请输入驾校名称(当前共%d家)",(int)models.count]]; } [groupDic removeAllObjects]; for (NSDictionary *dic in models) { NSString *letter = [Tools pinYinFromChinese:dic[@"SHORTNAME"]]; if (letter.length < 1) { continue; } letter = [letter substringToIndex:1]; if (![groupDic objectForKey:letter]) { [groupDic setObject:[NSMutableArray array] forKey:letter]; } [[groupDic objectForKey:letter] addObject:dic[@"SHORTNAME"]]; } [_contactTable reloadData]; } - (void)getSchPlacesWithDic:(NSDictionary *)dic { if (!dqbh || dqbh.length==0) { [self goAllCoachPlanVCWithDic:dic]; return; } if (![Util connectedToNetWork]) { [self goAllCoachPlanVCWithDic:dic]; return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"dqbh" Value:dqbh]; [arr addPro:@"jxbh" Value:dic[@"JXBH"]]; NSString* method = @"getSchPlaces"; [MBProgressHUD showLoadToView:self.view]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { [MBProgressHUD hideHUDForView:self.view]; if (!root) { [self goAllCoachPlanVCWithDic:dic]; return; } if ([root[@"code"] intValue] == 0) { if ([root[@"body"] count] < 1) { [self goAllCoachPlanVCWithDic:dic]; }else { FenceSiteVC *vc = [[FenceSiteVC alloc] init]; vc.dataArray = root[@"body"]; vc.dqbh = dqbh; [self navPushHideTabbarToVC:vc]; } }else { [self goAllCoachPlanVCWithDic:dic]; } }]; } - (void)goAllCoachPlanVCWithDic:(NSDictionary *)dic { AllCoachPlanVC* vc = [[AllCoachPlanVC alloc] init]; vc.searchJXBH = dic[@"JXBH"]; [self navPushHideTabbarToVC:vc]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end