// // SchoolListVC.m // LNManager // // Created by EchoShacolee on 2017/4/7. // Copyright © 2017年 lee. All rights reserved. // #import "SchoolListVC.h" #import "DBManager.h" #import "AssessorList.h" #import "CarList.h" #import "SchoolDetail.h" #import "CoachListVC.h" #import "SafeList.h" #import "StudentList.h" #import "CarList.h" #import "TeachLogsVC.h" #import "TerminalVC.h" @interface SchoolListVC () { //选择城市相关 UIPickerView *_pickerView; //地市数据库数据 NSMutableArray * _pickVList1; NSMutableArray * _pickVList2; NSMutableArray * _pickDataArr; NSString *_pickStr;//最后被选中的地区 NSInteger _proIndex;//记录选中城市的行数 NSString *_lastTitle;//(判断再次选取城市) // UIView * _mengBanView; UIToolbar *_toolBar; //记录当前地市 NSString * _dqbh; NSString * _qxbh; } @end @implementation SchoolListVC - (void)viewDidLoad { [super viewDidLoad]; self.dataArr = [NSMutableArray new]; [self customRightBtn]; _dqbh = MYAPPDELEGATE.userDic[@"dqbh"]; _qxbh = MYAPPDELEGATE.userDic[@"qxbh"]; [self getData]; __weak typeof(self) weakSelf = self; _block = ^{ [weakSelf getData]; }; } -(void)customRightBtn{ UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"选择地市" style:UIBarButtonItemStylePlain target:self action:@selector(setMengbanView:)]; item.tintColor = COLOR_THEME; NSString *userType = [NSString stringWithFormat:@"%@",MYAPPDELEGATE.userDic[@"userType"]]; //从数据库加载地市 NSMutableArray * sqliteArr = [[DBManager sharedManager]loadArea]; switch ([userType integerValue]) { case 1: { //省级 _pickVList1 = sqliteArr[0]; _pickVList2 = sqliteArr[1]; _pickDataArr = sqliteArr[2]; [_pickVList1 insertObject:@"全省" atIndex:0]; for (int i=0; i<_pickVList2.count; i++) { [_pickVList2[i] insertObject:@"全市" atIndex:0]; } [_pickVList2 insertObject:[NSMutableArray new] atIndex:0];//对应全省 self.navigationItem.rightBarButtonItem = item; } break; case 2: { //市级 NSString * cityName = @""; for (NSDictionary * dic in sqliteArr[2]) { if ([dic[@"code"] isEqualToString:MYAPPDELEGATE.userDic[@"dqbh"]]) { cityName = dic[@"name"]; } } NSUInteger index = [sqliteArr[0] indexOfObject:cityName]; _pickVList1 = [NSMutableArray arrayWithArray:@[cityName]]; _pickVList2 = [NSMutableArray arrayWithArray:@[sqliteArr[1][index]]]; _pickDataArr = sqliteArr[2]; [_pickVList2[0] insertObject:@"全市" atIndex:0]; self.navigationItem.rightBarButtonItem = item; } break; default: break; } } #pragma mark 地市选择视图 -(void)setMengbanView:(UIBarButtonItem *)sender{ UIWindow * window = [UIApplication sharedApplication].windows[0]; if (_mengBanView) { _mengBanView.alpha = 1.0; [window addSubview:_mengBanView]; return; } /*创建灰色背景*/ _mengBanView = [[UIView alloc] initWithFrame:KWINDOW.bounds]; _mengBanView.backgroundColor = [UIColor colorWithWhite:.01 alpha:.4]; [window addSubview:_mengBanView]; /*添加手势事件,移除View*/ UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView:)]; tapGesture.cancelsTouchesInView = NO; tapGesture.delegate = self; [_mengBanView addGestureRecognizer:tapGesture]; // UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(kSize.width/3.0, kNavOffSet, kSize.width*2.0/3, 200+44)]; contentView.layer.cornerRadius = 10; contentView.layer.masksToBounds = YES; [_mengBanView addSubview:contentView]; //toolbar UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kSize.width*2.0/3, 44)]; pickerDateToolbar.barStyle = UIBarStyleDefault; // [pickerDateToolbar sizeToFit]; _toolBar = pickerDateToolbar; // UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(toolBarCanelClick)]; cancelBtn.tintColor = COLOR_THEME; // UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(toolBarDoneClick)]; doneBtn.tintColor = COLOR_THEME; // UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; [pickerDateToolbar setItems:@[flexSpace,cancelBtn,flexSpace,flexSpace,flexSpace,flexSpace,doneBtn,flexSpace] animated:YES]; [contentView addSubview:pickerDateToolbar]; // 选择框 _pickerView = [[UIPickerView alloc] init]; _pickerView.frame = CGRectMake(0, 44, kSize.width*2.0/3, 200); _pickerView.backgroundColor = RGB_COLOR(246, 246, 246); _pickerView.showsSelectionIndicator=YES; _pickerView.dataSource = self; _pickerView.delegate = self; _proIndex = 0;//第一列默认选中第0行 [contentView addSubview:_pickerView]; } -(void)toolBarCanelClick{ [self dismissContactViewWithIsSure:NO]; } -(void)toolBarDoneClick{ [self dismissContactViewWithIsSure:YES]; //其实这一块可以放上面那行方法里面,不过为了让它看起来不那么复杂,就放这里吧 if (_pickStr) { self.navigationItem.rightBarButtonItem.title = _pickStr; }else{ self.navigationItem.rightBarButtonItem.title = _pickVList1[0]; return;//_pickstr不存在,即没有滑动 } if ([_lastTitle isEqualToString:_pickStr]) { return; } if ([_pickStr isEqualToString:@"全省"]) { _dqbh = @""; _qxbh = @""; [self getData]; }else if ([_pickVList1 containsObject:_pickStr]){//全市 for (NSMutableDictionary *dic in _pickDataArr) { if ([dic[@"name"] isEqualToString:_pickStr]) { _dqbh = dic[@"code"]; _qxbh = @""; [self getData]; return; } } }else{ for (NSMutableDictionary *dic in _pickDataArr) { if ([dic[@"name"] isEqualToString:_pickStr]) { _dqbh = dic[@"parent_code"]; _qxbh = dic[@"code"]; [self getData]; } } } return; } #pragma mark - tap手势代理方法 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { CGPoint location = [touch locationInView:self.view]; CGRect frame1 = [self.view convertRect:_toolBar.frame fromView:_mengBanView.subviews[0]]; CGRect frame2 = [self.view convertRect:frame1 fromView:_mengBanView]; if(CGRectContainsPoint(frame2, location)) { return NO; } return YES; } #pragma mark - 手势点击事件,移除View - (void)dismissContactView:(UITapGestureRecognizer *)tapGesture{ [self dismissContactViewWithIsSure:NO]; } -(void)dismissContactViewWithIsSure:(BOOL)sure { //如果是取消 if (!sure) { //取消(取消之前恢复pickerview原状态) //作相应处理 _pickStr = _lastTitle; if ([_lastTitle isEqualToString:@"全省"] || !_lastTitle) { _proIndex = 0; [_pickerView reloadAllComponents]; [_pickerView selectRow:0 inComponent:0 animated:NO]; [_pickerView selectRow:0 inComponent:1 animated:NO]; }else if ([_pickVList1 containsObject:_lastTitle]){//全市 _proIndex = [_pickVList1 indexOfObject:_lastTitle]; [_pickerView reloadAllComponents]; [_pickerView selectRow:_proIndex inComponent:0 animated:NO]; [_pickerView selectRow:0 inComponent:1 animated:NO]; }else{ //在第二列的位置 NSInteger index2 = 0; for (int i=0; i<_pickVList2.count; i++) { if ([_pickVList2[i] containsObject:_lastTitle]) { _proIndex = i;//第一列位置 index2 = [_pickVList2[i] indexOfObject:_lastTitle];//第二列位置 break; } } [_pickerView reloadAllComponents]; [_pickerView selectRow:_proIndex inComponent:0 animated:NO]; [_pickerView selectRow:index2 inComponent:1 animated:NO]; } } [UIView animateWithDuration:0.5 animations:^{ _mengBanView.alpha = 0; } completion:^(BOOL finished) { [_mengBanView removeFromSuperview]; }]; } #pragma mark 数据请求 -(void)getData{ NSMutableDictionary * mdic = [NSMutableDictionary new]; [mdic setValue:_dqbh forKey:@"dqbh"]; [mdic setValue:_qxbh forKey:@"qxbh"]; [mdic setValue:@"" forKey:@"inscode"];//驾校编号 [mdic setValue:@"" forKey:@"jxmc"]; [mdic setValue:@"" forKey:@"isPage"]; [mdic setValue:@"" forKey:@"pageSize"]; [mdic setValue:@"" forKey:@"currentPage"]; [mdic setValue:@"1" forKey:@"auditStatus"]; _requsetDic = mdic; [self getDataWithDic:mdic method:@"schools" block:^(NSDictionary *successdic) { _lastTitle = _pickStr;//记录当前已确定地市 // [_sectionTitles removeAllObjects]; [self.dataArr removeAllObjects]; [_dataSource removeAllObjects]; NSMutableArray * mArr = [NSMutableArray new];//这里不直接对self.dataarr操作是减少子类重写set方法时的频繁调用 NSArray * arr = successdic[@"body"]; if (arr.count != 0) { self.holderV.hidden = YES; } for (NSDictionary * dic in arr) { if (![_sectionTitles containsObject:dic[@"ZM"]]) { [_sectionTitles addObject:dic[@"ZM"]]; [mArr addObject:[NSMutableArray new]]; } [[mArr lastObject] addObject:dic]; } self.dataArr = mArr;//子类用 _dataSource = mArr;//父类用 [self.tableView reloadData]; }]; } #pragma mark UIPickerViewDataSource // pickerView 列数 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; } // pickerView 每列个数 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if (component == 0) { return _pickVList1.count; } return [_pickVList2[_proIndex] count]; } #pragma mark UIPickerViewDelegate // 每列宽度 - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { if (component == 0) { return 70; } return 130; } // 返回选中的行 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { // NSLog(@"%ld,%ld",(long)component,(long)row); if (component == 0) { _proIndex = row; [pickerView reloadComponent:1]; [_pickerView selectRow:0 inComponent:1 animated:NO]; if (row == 0) { _pickStr = @"全省"; }else{ _pickStr = _pickVList1[_proIndex]; } } if ((_proIndex != 0 && component == 1)|| _pickVList1.count == 1) { if (row != 0) { _pickStr = _pickVList2[_proIndex][row]; }else{ _pickStr = _pickVList1[_proIndex]; } } } //设置风格 -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ //设置分割线的颜色 for(UIView *singleLine in pickerView.subviews) { if (singleLine.frame.size.height < 1) { singleLine.backgroundColor = [UIColor blackColor]; } } //设置文字的属性 UILabel *genderLabel = [UILabel new]; genderLabel.textAlignment = NSTextAlignmentCenter; if (component == 0) { genderLabel.text = [_pickVList1 objectAtIndex:row]; }else{ genderLabel.text = [_pickVList2[_proIndex] objectAtIndex:row]; } genderLabel.textColor = [UIColor redColor]; return genderLabel; } #pragma mark 重写Cell出列方法 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath]; if ([self.dataArr[indexPath.section] count] > 0) { cell.textLabel.text = self.dataArr[indexPath.section][indexPath.row][@"TSI_SHORTNAME"]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //驾校编号 NSString * inscode = self.dataArr[indexPath.section][indexPath.row][@"TSI_INSCODE"]; switch (self.type) { case 0: { //驾校详情 SchoolDetail * vc = [[SchoolDetail alloc]init]; [_requsetDic setValue:inscode forKey:@"inscode"]; vc.requesetDic = _requsetDic; [self.navigationController pushViewController:vc animated:YES]; } break; case 1: { //教练 CoachListVC * vc = [[CoachListVC alloc]init]; vc.shcDic = self.dataArr[indexPath.section][indexPath.row]; [self.navigationController pushViewController:vc animated:YES]; } break; case 2: { //考核员 AssessorList * vc = [[AssessorList alloc]init]; vc.shcDic = self.dataArr[indexPath.section][indexPath.row]; [self.navigationController pushViewController:vc animated:YES]; } break; case 3: { //安全员 SafeList * vc = [[SafeList alloc]init]; vc.shcDic = self.dataArr[indexPath.section][indexPath.row]; [self.navigationController pushViewController:vc animated:YES]; } break; case 4: { //学员 StudentList * vc = [[StudentList alloc]init]; vc.shcDic = self.dataArr[indexPath.section][indexPath.row]; [self.navigationController pushViewController:vc animated:YES]; } break; case 5: { //车辆 CarList * vc = [[CarList alloc]init]; vc.shcDic = self.dataArr[indexPath.section][indexPath.row]; [self.navigationController pushViewController:vc animated:YES]; } break; case 6: { //学时审核 // XueShiCheckList * vc = [[XueShiCheckList alloc]init]; // vc.shcDic = self.dataArr[indexPath.section][indexPath.row]; // [self.navigationController pushViewController:vc animated:YES]; } break; case 7: { //教学日志 TeachLogsVC * vc = [[TeachLogsVC alloc]init]; vc.schDic = self.dataArr[indexPath.section][indexPath.row]; [self.navigationController pushViewController:vc animated:YES]; } break; case 8: { //通知下发 初始化判断 //选取驾校发送通知 } break; case 9: { //终端管理 TerminalVC * vc = [[TerminalVC alloc]init]; vc.shcDic = self.dataArr[indexPath.section][indexPath.row]; [self.navigationController pushViewController:vc animated:YES]; } break; default: break; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end