// // Notify_SelectCityVC.m // LNManager // // Created by EchoShacolee on 2017/6/21. // Copyright © 2017年 lee. All rights reserved. // #import "Notify_SelectCityVC.h" #import "DBManager.h" #import "Notify_SelectCityCell.h" #import "SelectCell.h" @interface Notify_SelectCityVC () { //地市数据库数据 NSArray *_qxArr; //选择相关 UIView *_btnView; UIButton *_notifyBtn; //用户级别 NSInteger _level;//1省级,2市级,3.区级 //记录每一组的状态(@0:关闭,@1:展开) NSMutableArray *_sectionStatus; //记录xx市被选中 NSMutableArray *_dqSlcIndexPs; //记录xx区县被选中 NSMutableArray *_selectedIndexPathes; } @end @implementation Notify_SelectCityVC - (void)viewDidLoad { [super viewDidLoad]; //实例化 _qxArr = [[NSArray alloc]init]; _dqSlcIndexPs = [[NSMutableArray alloc] init];; _selectedIndexPathes = [[NSMutableArray alloc] init]; _sectionStatus = [[NSMutableArray alloc] init]; _level = [[NSString stringWithFormat:@"%@",MYAPPDELEGATE.userDic[@"userType"]] integerValue]; [self getDataFromLocal]; [self myInit]; } -(void)myInit{ _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; _tableView.dataSource = self; _tableView.delegate = self; self.tableView.estimatedRowHeight = 0; self.tableView.estimatedSectionHeaderHeight = 0; self.tableView.estimatedSectionFooterHeight = 0; [self.view addSubview:_tableView]; //创建选择按钮 [self createSelectBtn]; } -(void)createSelectBtn{ if (_level == 3) { UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kSize.width-40, 60)]; lab.center = CGPointMake(kSize.width/2, 250); lab.numberOfLines = 0; lab.backgroundColor = [UIColor whiteColor]; lab.text = @"当前用户为区县级别,无下属地市,请选择驾校发送"; lab.textColor = [UIColor darkGrayColor]; lab.textAlignment = NSTextAlignmentCenter; [self.view addSubview:lab]; UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(kSize.width-120, CGRectGetMaxY(lab.frame)+10, 100, 40); [button setTitle:@"< 通知驾校" forState:UIControlStateNormal]; [button setTitleColor:COLOR_THEME forState:UIControlStateNormal]; [button addTarget:self action:@selector(goSch) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; return; } _notifyBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _notifyBtn.backgroundColor = COLOR_THEME; _notifyBtn.frame = CGRectMake(kSize.width-80, kSize.height-200, 60, 60); [_notifyBtn setTitle:@"选择\n地市" forState:UIControlStateNormal]; [_notifyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _notifyBtn.titleLabel.numberOfLines = 0; _notifyBtn.layer.masksToBounds = YES; _notifyBtn.layer.cornerRadius = 30; [_notifyBtn addTarget:self action:@selector(notifiBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_notifyBtn]; } #pragma mark - 获取数据 -(void)getDataFromLocal{ NSArray * sqliteArr = [[DBManager sharedManager]getAreaFromSqlite]; NSMutableArray *arr1 = sqliteArr[0]; NSMutableArray *arr2 = sqliteArr[1]; if (_level == 1) { for (int i=0; i 0) { [self sendNotifyWithIds:allId]; }else{ [self showMsgByAlertVCWithString:@"请选择地市"]; } } -(void)creatBtnView{ UIView* btnView = [[UIView alloc]initWithFrame:CGRectMake(kSize.width-180, kSize.height-250, 80, 110)]; _btnView = btnView; [self.view addSubview:btnView]; UIButton *citySlc = [UIButton buttonWithType:UIButtonTypeCustom]; citySlc.backgroundColor = [UIColor lightGrayColor]; citySlc.frame = CGRectMake(0, 0, 80, 30); [citySlc setTitle:@"全选地市" forState:UIControlStateNormal]; [citySlc setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [citySlc setTitleColor:COLOR_THEME forState:UIControlStateSelected]; [citySlc addTarget:self action:@selector(allDqClick:) forControlEvents:UIControlEventTouchUpInside]; citySlc.hidden = _level == 1 ? NO : YES; [btnView addSubview:citySlc]; UIButton *allBtn = [UIButton buttonWithType:UIButtonTypeCustom]; allBtn.backgroundColor = [UIColor lightGrayColor]; allBtn.frame = CGRectMake(0, 40, 80, 30); [allBtn setTitle:@"全选区县" forState:UIControlStateNormal]; [allBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [allBtn setTitleColor:COLOR_THEME forState:UIControlStateSelected]; [allBtn addTarget:self action:@selector(allQxClick:) forControlEvents:UIControlEventTouchUpInside]; [btnView addSubview:allBtn]; UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; cancelBtn.backgroundColor = [UIColor lightGrayColor]; cancelBtn.frame = CGRectMake(0, 80, 80, 30); [cancelBtn setTitle:@"取消" forState:UIControlStateNormal]; [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside]; [btnView addSubview:cancelBtn]; } -(void)sendNotifyWithIds:(NSMutableArray *)ids{ if (self.blcok) { self.blcok(ids); } } -(void)allDqClick:(UIButton *)button{ button.selected = !button.selected; //不做展开关闭组的操作 // //展开所有分组 // [self createStatusArray]; // //刷新指定的组的数据 // [self.tableView reloadData]; if (button.selected == YES) { // //获取表格视图内容的尺寸 CGSize size = _tableView.contentSize; CGRect rect = CGRectMake(0, 0, size.width, size.height); //获取指定区域的cell的indexPath NSArray *indexPathes = [_tableView indexPathsForRowsInRect:rect]; NSMutableArray * allIndexPath = [NSMutableArray arrayWithArray:indexPathes]; [_dqSlcIndexPs removeAllObjects]; for (NSIndexPath *indexPath in allIndexPath) { //使用代码方式选中一行 if (indexPath.row == 0 && _level == 1) { [_dqSlcIndexPs addObject:indexPath]; [_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; } } }else{ for (NSIndexPath *indexPath in _dqSlcIndexPs) { //使用代码方式取消选中 [_tableView deselectRowAtIndexPath:indexPath animated:YES]; } [_dqSlcIndexPs removeAllObjects]; } } -(void)allQxClick:(UIButton *)button{ button.selected = !button.selected; //展开所有分组 NSNumber *number = @1; for (int i=0; i<_sectionStatus.count; i++) { [_sectionStatus replaceObjectAtIndex:i withObject:number]; } [self.tableView reloadData]; if (button.selected == YES) { // //获取表格视图内容的尺寸 CGSize size = _tableView.contentSize; CGRect rect = CGRectMake(0, 0, size.width, size.height); //获取指定区域的cell的indexPath NSArray *indexPathes = [_tableView indexPathsForRowsInRect:rect]; NSMutableArray * allIndexPath = [NSMutableArray arrayWithArray:indexPathes]; _selectedIndexPathes = [allIndexPath mutableCopy]; for (NSIndexPath *indexPath in allIndexPath) { if (indexPath.row == 0 && _level == 1) { [_selectedIndexPathes removeObject:indexPath]; continue; } [_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; } }else{ //关闭所有分组 [self createStatusArray]; [self.tableView reloadData]; for (NSIndexPath *indexPath in _selectedIndexPathes) { //使用代码方式取消选中 [_tableView deselectRowAtIndexPath:indexPath animated:YES]; } [_selectedIndexPathes removeAllObjects]; } } - (void)cancelClick:(UIButton *)sender { [_btnView removeFromSuperview]; _btnView = nil; [self.tableView setEditing:NO]; [_notifyBtn setTitle:@"选择\n地市" forState:UIControlStateNormal]; for (NSIndexPath *indexPath in _selectedIndexPathes) { //使用代码方式取消选中一行 [_tableView deselectRowAtIndexPath:indexPath animated:YES]; } //清空选中cell的记录数组 [_selectedIndexPathes removeAllObjects]; } #pragma mark tableview代理相关 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return _qxArr.count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if ([_sectionStatus[section] isEqualToNumber:@0]) { return 1; } return [_qxArr[section] count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0 && _level == 1) { Notify_SelectCityCell * cell = [Notify_SelectCityCell cellForTableView:tableView]; cell.title = _qxArr[indexPath.section][indexPath.row][@"name"]; [cell updateWithStatus:[_sectionStatus[indexPath.section] boolValue]]; if (self.tableView.editing && [_dqSlcIndexPs containsObject:indexPath]) { [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; } cell.openBlock = ^{ NSNumber *number = [_sectionStatus[indexPath.section] isEqualToNumber:@0] ? @1 : @0; //改变状态 [_sectionStatus replaceObjectAtIndex:indexPath.section withObject:number]; //刷新指定的组的数据 [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic]; }; return cell; } SelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"]; if (!cell) { cell = [[SelectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"]; } cell.textLabel.text = _qxArr[indexPath.section][indexPath.row][@"name"]; if (self.tableView.editing && [_selectedIndexPathes containsObject:indexPath]) { [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; } return cell; } #pragma mark -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 10; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.1f; } #pragma mark - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleInsert |UITableViewCellEditingStyleDelete; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (!tableView.editing) { return; } if (indexPath.row == 0 && _level == 1) { if (![_dqSlcIndexPs containsObject:indexPath]) { [_dqSlcIndexPs addObject:indexPath]; } return; } if (![_selectedIndexPathes containsObject:indexPath]) { [_selectedIndexPathes addObject:indexPath]; } return; } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { if (!tableView.editing) { return; } if (indexPath.row == 0 && _level == 1) { if ([_dqSlcIndexPs containsObject:indexPath]) { [_dqSlcIndexPs removeObject:indexPath]; } return; } if ([_selectedIndexPathes containsObject:indexPath]) { [_selectedIndexPathes removeObject:indexPath]; } } - (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