// // AllCoachPlanVC.m // jiaPei // // Created by apple on 16/8/18. // Copyright © 2016年 JCZ. All rights reserved. // #import "AllCoachPlanVC.h" #import "CoachPlanVC.h" #import "HolderView.h" #import "AllCoachCell.h" @interface AllCoachPlanVC () { HolderView *holderV; UITableView *mainTableView; UITextField *textField; UISearchBar *_searchBar; UISearchController *_searchController; BOOL isResult; NSArray *dataArray; NSMutableArray *resultArray; } @end @implementation AllCoachPlanVC - (void)viewDidLoad { [super viewDidLoad]; [self myInit]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.view endEditing:YES]; //这个一定要写 要不然会在退出去下一个页面时候 搜索栏有一定时间的存留 if (_searchController.active) { _searchController.active = NO; if (_searchController.searchBar && _searchController.searchBar.superview) { [_searchController.searchBar removeFromSuperview]; } } } -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; self.tabBarController.tabBar.hidden = YES; } -(void)myInit{ [self setTitle:@"教练列表"]; [self.view setBackgroundColor:backGroundColor]; if (_isNotification == NO) { //正常状态 [self configNavigationBar]; } else { //推送状态 要将根视图变回去 UIBarButtonItem* backBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"question_pre_checked_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissNavgation)]; [backBtn setTintColor:defGreen]; self.navigationController.navigationBar.translucent = NO; [self.navigationItem setLeftBarButtonItem:backBtn]; } isResult = NO; dataArray = [NSArray array]; resultArray = [NSMutableArray array]; // UIView *searchBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSize.width, 60)]; // searchBar.backgroundColor = backGroundColor; // [self.view addSubview:searchBar]; // [searchBar addViewWithRect:CGRectMake(5, 59, kSize.width - 10, 1) Color:lineColor]; // // textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, kSize.width - 140, 40)]; // textField.placeholder = @"请输入教练姓名"; // textField.borderStyle = UITextBorderStyleRoundedRect; // textField.clearButtonMode = UITextFieldViewModeAlways; // [searchBar addSubview:textField]; // // UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // searchBtn.frame = CGRectMake(kSize.width - 100, 12, 80, 36); // searchBtn.backgroundColor = [UIColor brownColor]; // [searchBtn setTitle:@"查询" TextColor:kTitleColor font:FontTitle fotState:UIControlStateNormal]; // [searchBtn borderColor:lineColor width:1 cornorRadios:18]; // [searchBtn target:self tag:1]; // [searchBar addSubview:searchBtn]; mainTableView = [[UITableView alloc] initWithFrame:kFrame style:UITableViewStylePlain]; // mainTableView.y += 60; mainTableView.height -= kNavOffSet; mainTableView.delegate = self; mainTableView.dataSource = self; mainTableView.rowHeight = 110; mainTableView.estimatedSectionHeaderHeight = 0; mainTableView.estimatedSectionFooterHeight = 0; [self.view addSubview:mainTableView]; 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]; mainTableView.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]; //设置类型的标题 //[_searchBar setScopeButtonTitles:@[@"按姓名查找",@"按电话查找"]]; //改变索引的颜色 //_contactTable.sectionIndexColor = [UIColor blueColor]; //改变索引选中的背景颜色 //设置表的表头 [mainTableView setTableHeaderView:_searchBar]; //搜索显示器 Display显示 // UISearchDisplayController * displayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self]; // // [displayController setDelegate:self]; // [displayController setSearchResultsDataSource:self]; // [displayController setSearchResultsDelegate:self]; } holderV = [[HolderView alloc] initWithFrame:mainTableView.frame]; [holderV freshBlock:^{ [self getCoachInfos]; }]; [self.view addSubview:holderV]; [self getCoachInfos]; } -(void)dismissNavgation { //推送过来 返回主页面 [myDelegate gotoLoad]; } -(void)btnClick:(UIButton *)sender { [self.view endEditing:YES]; [resultArray removeAllObjects]; if (textField.text.length < 1) { isResult = NO; }else{ isResult = YES; for (NSDictionary *dic in dataArray) { if ([dic[@"NAME"] containsString:textField.text] || [dic[@"CARTYPE"] containsString:textField.text] || [dic[@"SEX"] containsString:textField.text]) { [resultArray addObject:dic]; } } } [mainTableView reloadData]; } #pragma mark maintableview delegate -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (isResult) { return resultArray.count; } return dataArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { AllCoachCell *cell = [tableView dequeueReusableCellWithIdentifier:@"allCoach"]; if (cell == nil) { cell = [[AllCoachCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"allCoach"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } NSDictionary *dic; if (isResult) { dic = resultArray[indexPath.row]; }else{ dic = dataArray[indexPath.row]; } cell.dataDic = dic; cell.superVC = self; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { CoachPlanVC *vc = [[CoachPlanVC alloc] init]; if (isResult) { vc.coachDic = resultArray[indexPath.row]; }else{ vc.coachDic = dataArray[indexPath.row]; } [self navPushHideTabbarToVC:vc]; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { //[self.view endEditing:YES]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { if (_searchController.searchBar) { [_searchController.searchBar endEditing:YES]; } }else{ if (_searchBar) { [_searchBar endEditing:YES]; } } } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return .1; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return .1; } #pragma mark UISearchDisplayController 代理方法 ////iOS7搜索的时候 会调用这个方法 //- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString //{ // //NSLog(@"searchString====%@",searchString); // [resultArray removeAllObjects]; // // if (searchString.length < 1) { // isResult = NO; // }else{ // isResult = YES; // for (NSDictionary *dic in dataArray) { // // NSString *numString = @"1234567890"; // if (searchString.length == 1 && [numString containsString:searchString]) { // // if ([dic[@"STAR"] integerValue] > [numString integerValue]) { // // [resultArray addObject:dic]; // } // continue; // } // // if ([dic[@"NAME"] containsString:searchString] || [dic[@"CARTYPE"] containsString:searchString] || [dic[@"SEX"] containsString:searchString] || [dic[@"MOBILE"] containsString:searchString]) { // // [resultArray addObject:dic]; // } // } // } // // [mainTableView reloadData]; // // //// for (NSString * info in _tableSourceArray) //// { //// if ([info rangeOfString:searchString].location != NSNotFound) //// { //// [_resultArray addObject:info]; //// } //// } //// //// [_contactTable reloadData]; // // return YES; //} ////点击不同的选择范围的时候调用 //-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption // //{ // [self searchDisplayController:controller shouldReloadTableForSearchString:_searchBar.text]; // return YES; //} //iOS8搜索调用的方法 - (void)updateSearchResultsForSearchController:(UISearchController *)searchController { [resultArray removeAllObjects]; NSString *searchString = _searchController.searchBar.text; if (searchString.length < 1) { isResult = NO; }else{ isResult = YES; for (NSDictionary *dic in dataArray) { NSString *numString = @"1234567890"; if (searchString.length == 1 && [numString containsString:searchString]) { if ([dic[@"STAR"] intValue] >= [searchString intValue]) { [resultArray addObject:dic]; } continue; } if ([dic[@"NAME"] containsString:searchString] || [dic[@"CARTYPE"] containsString:searchString] || [dic[@"SEX"] containsString:searchString] || [dic[@"MOBILE"] containsString:searchString]) { [resultArray addObject:dic]; } } } [mainTableView reloadData]; } #pragma mark 异步获取教练信息 -(void)getCoachInfos { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr=[NSMutableArray array]; NSString* method = @"getSchPlaceCoachs"; if (_fenceSiteID.length > 0) { [arr addPro:@"dqbh" Value:_dqbh]; [arr addPro:@"placeId" Value:_fenceSiteID]; }else { [arr addPro:@"jxbh" Value:_searchJXBH]; method = @"getCoachInfos"; } [MBProgressHUD showLoadToView:self.view]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:self.view]; [holderV setHidden:NO]; if (!root) { ShowMsgFailed(); return; } if ([root[@"code"] isEqualToString:@"1"]) { ShowMsg(root[@"body"]); return; } if ([root[@"body"] count] < 1) { return; } dataArray = root[@"body"]; [holderV setHidden:YES]; [mainTableView reloadData]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end