123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- //
- // 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 ()<UITableViewDataSource,UITableViewDelegate>
- {
- //地市数据库数据
- 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<arr1.count; i++) {
- [arr2[i] insertObject:arr1[i] atIndex:0];
- }
- _qxArr = arr2;
-
- //创建状态数组(仅省级)
- [self createStatusArray];
-
- }else if (_level == 2){
-
- NSString *dqbh = MYAPPDELEGATE.userDic[@"dqbh"];
- NSMutableArray *arr = [NSMutableArray new];
-
- for (int i=0; i<[arr1 count]; i++) {
- NSDictionary *dic = arr1[i];
- if ([dic[@"code"] isEqualToString:dqbh]) {
- arr = [NSMutableArray arrayWithArray:arr2[i]];
- }
- }
- _qxArr = @[arr];
-
- [_sectionStatus addObject:@1];
-
- }else{
- //区级
-
- }
-
- }
- - (void)createStatusArray {
-
- [_sectionStatus removeAllObjects];
-
- NSInteger count = [_qxArr count];
- //初始化(关闭状态)
- for (NSInteger i=0; i<count; i++) {
- [_sectionStatus addObject:@0];
- }
- }
- #pragma mark 按钮点击
- -(void)goSch{
-
- if (self.goSchBlock) {
- self.goSchBlock();
- }
- }
- -(void)notifiBtnClick:(UIButton *)sender{
-
- if (self.tableView.editing == NO) {
-
- //清空所有选择
- [_dqSlcIndexPs removeAllObjects];
- [_selectedIndexPathes removeAllObjects];
-
- [_notifyBtn setTitle:@"完成" forState:UIControlStateNormal];
- [self.tableView setEditing:YES];
- [self creatBtnView];
- return;
- }
-
- NSMutableArray * allId = [NSMutableArray new];
- for (NSIndexPath *indexPath in _selectedIndexPathes) {
- [allId addObject:_qxArr[indexPath.section][indexPath.row][@"code"]];
- }
- for (NSIndexPath *indexPath in _dqSlcIndexPs) {
- [allId addObject:_qxArr[indexPath.section][indexPath.row][@"code"]];
- }
-
- if (allId.count > 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
|