Notify_SelectCityVC.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. //
  2. // Notify_SelectCityVC.m
  3. // LNManager
  4. //
  5. // Created by EchoShacolee on 2017/6/21.
  6. // Copyright © 2017年 lee. All rights reserved.
  7. //
  8. #import "Notify_SelectCityVC.h"
  9. #import "DBManager.h"
  10. #import "Notify_SelectCityCell.h"
  11. #import "SelectCell.h"
  12. @interface Notify_SelectCityVC ()<UITableViewDataSource,UITableViewDelegate>
  13. {
  14. //地市数据库数据
  15. NSArray *_qxArr;
  16. //选择相关
  17. UIView *_btnView;
  18. UIButton *_notifyBtn;
  19. //用户级别
  20. NSInteger _level;//1省级,2市级,3.区级
  21. //记录每一组的状态(@0:关闭,@1:展开)
  22. NSMutableArray *_sectionStatus;
  23. //记录xx市被选中
  24. NSMutableArray *_dqSlcIndexPs;
  25. //记录xx区县被选中
  26. NSMutableArray *_selectedIndexPathes;
  27. }
  28. @end
  29. @implementation Notify_SelectCityVC
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. //实例化
  33. _qxArr = [[NSArray alloc]init];
  34. _dqSlcIndexPs = [[NSMutableArray alloc] init];;
  35. _selectedIndexPathes = [[NSMutableArray alloc] init];
  36. _sectionStatus = [[NSMutableArray alloc] init];
  37. _level = [[NSString stringWithFormat:@"%@",MYAPPDELEGATE.userDic[@"userType"]] integerValue];
  38. [self getDataFromLocal];
  39. [self myInit];
  40. }
  41. -(void)myInit{
  42. _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
  43. _tableView.dataSource = self;
  44. _tableView.delegate = self;
  45. self.tableView.estimatedRowHeight = 0;
  46. self.tableView.estimatedSectionHeaderHeight = 0;
  47. self.tableView.estimatedSectionFooterHeight = 0;
  48. [self.view addSubview:_tableView];
  49. //创建选择按钮
  50. [self createSelectBtn];
  51. }
  52. -(void)createSelectBtn{
  53. if (_level == 3) {
  54. UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kSize.width-40, 60)];
  55. lab.center = CGPointMake(kSize.width/2, 250);
  56. lab.numberOfLines = 0;
  57. lab.backgroundColor = [UIColor whiteColor];
  58. lab.text = @"当前用户为区县级别,无下属地市,请选择驾校发送";
  59. lab.textColor = [UIColor darkGrayColor];
  60. lab.textAlignment = NSTextAlignmentCenter;
  61. [self.view addSubview:lab];
  62. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  63. button.frame = CGRectMake(kSize.width-120, CGRectGetMaxY(lab.frame)+10, 100, 40);
  64. [button setTitle:@"< 通知驾校" forState:UIControlStateNormal];
  65. [button setTitleColor:COLOR_THEME forState:UIControlStateNormal];
  66. [button addTarget:self action:@selector(goSch) forControlEvents:UIControlEventTouchUpInside];
  67. [self.view addSubview:button];
  68. return;
  69. }
  70. _notifyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  71. _notifyBtn.backgroundColor = COLOR_THEME;
  72. _notifyBtn.frame = CGRectMake(kSize.width-80, kSize.height-200, 60, 60);
  73. [_notifyBtn setTitle:@"选择\n地市" forState:UIControlStateNormal];
  74. [_notifyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  75. _notifyBtn.titleLabel.numberOfLines = 0;
  76. _notifyBtn.layer.masksToBounds = YES;
  77. _notifyBtn.layer.cornerRadius = 30;
  78. [_notifyBtn addTarget:self action:@selector(notifiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  79. [self.view addSubview:_notifyBtn];
  80. }
  81. #pragma mark - 获取数据
  82. -(void)getDataFromLocal{
  83. NSArray * sqliteArr = [[DBManager sharedManager]getAreaFromSqlite];
  84. NSMutableArray *arr1 = sqliteArr[0];
  85. NSMutableArray *arr2 = sqliteArr[1];
  86. if (_level == 1) {
  87. for (int i=0; i<arr1.count; i++) {
  88. [arr2[i] insertObject:arr1[i] atIndex:0];
  89. }
  90. _qxArr = arr2;
  91. //创建状态数组(仅省级)
  92. [self createStatusArray];
  93. }else if (_level == 2){
  94. NSString *dqbh = MYAPPDELEGATE.userDic[@"dqbh"];
  95. NSMutableArray *arr = [NSMutableArray new];
  96. for (int i=0; i<[arr1 count]; i++) {
  97. NSDictionary *dic = arr1[i];
  98. if ([dic[@"code"] isEqualToString:dqbh]) {
  99. arr = [NSMutableArray arrayWithArray:arr2[i]];
  100. }
  101. }
  102. _qxArr = @[arr];
  103. [_sectionStatus addObject:@1];
  104. }else{
  105. //区级
  106. }
  107. }
  108. - (void)createStatusArray {
  109. [_sectionStatus removeAllObjects];
  110. NSInteger count = [_qxArr count];
  111. //初始化(关闭状态)
  112. for (NSInteger i=0; i<count; i++) {
  113. [_sectionStatus addObject:@0];
  114. }
  115. }
  116. #pragma mark 按钮点击
  117. -(void)goSch{
  118. if (self.goSchBlock) {
  119. self.goSchBlock();
  120. }
  121. }
  122. -(void)notifiBtnClick:(UIButton *)sender{
  123. if (self.tableView.editing == NO) {
  124. //清空所有选择
  125. [_dqSlcIndexPs removeAllObjects];
  126. [_selectedIndexPathes removeAllObjects];
  127. [_notifyBtn setTitle:@"完成" forState:UIControlStateNormal];
  128. [self.tableView setEditing:YES];
  129. [self creatBtnView];
  130. return;
  131. }
  132. NSMutableArray * allId = [NSMutableArray new];
  133. for (NSIndexPath *indexPath in _selectedIndexPathes) {
  134. [allId addObject:_qxArr[indexPath.section][indexPath.row][@"code"]];
  135. }
  136. for (NSIndexPath *indexPath in _dqSlcIndexPs) {
  137. [allId addObject:_qxArr[indexPath.section][indexPath.row][@"code"]];
  138. }
  139. if (allId.count > 0) {
  140. [self sendNotifyWithIds:allId];
  141. }else{
  142. [self showMsgByAlertVCWithString:@"请选择地市"];
  143. }
  144. }
  145. -(void)creatBtnView{
  146. UIView* btnView = [[UIView alloc]initWithFrame:CGRectMake(kSize.width-180, kSize.height-250, 80, 110)];
  147. _btnView = btnView;
  148. [self.view addSubview:btnView];
  149. UIButton *citySlc = [UIButton buttonWithType:UIButtonTypeCustom];
  150. citySlc.backgroundColor = [UIColor lightGrayColor];
  151. citySlc.frame = CGRectMake(0, 0, 80, 30);
  152. [citySlc setTitle:@"全选地市" forState:UIControlStateNormal];
  153. [citySlc setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  154. [citySlc setTitleColor:COLOR_THEME forState:UIControlStateSelected];
  155. [citySlc addTarget:self action:@selector(allDqClick:) forControlEvents:UIControlEventTouchUpInside];
  156. citySlc.hidden = _level == 1 ? NO : YES;
  157. [btnView addSubview:citySlc];
  158. UIButton *allBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  159. allBtn.backgroundColor = [UIColor lightGrayColor];
  160. allBtn.frame = CGRectMake(0, 40, 80, 30);
  161. [allBtn setTitle:@"全选区县" forState:UIControlStateNormal];
  162. [allBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  163. [allBtn setTitleColor:COLOR_THEME forState:UIControlStateSelected];
  164. [allBtn addTarget:self action:@selector(allQxClick:) forControlEvents:UIControlEventTouchUpInside];
  165. [btnView addSubview:allBtn];
  166. UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  167. cancelBtn.backgroundColor = [UIColor lightGrayColor];
  168. cancelBtn.frame = CGRectMake(0, 80, 80, 30);
  169. [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
  170. [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  171. [cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
  172. [btnView addSubview:cancelBtn];
  173. }
  174. -(void)sendNotifyWithIds:(NSMutableArray *)ids{
  175. if (self.blcok) {
  176. self.blcok(ids);
  177. }
  178. }
  179. -(void)allDqClick:(UIButton *)button{
  180. button.selected = !button.selected;
  181. //不做展开关闭组的操作
  182. // //展开所有分组
  183. // [self createStatusArray];
  184. // //刷新指定的组的数据
  185. // [self.tableView reloadData];
  186. if (button.selected == YES) {
  187. // //获取表格视图内容的尺寸
  188. CGSize size = _tableView.contentSize;
  189. CGRect rect = CGRectMake(0, 0, size.width, size.height);
  190. //获取指定区域的cell的indexPath
  191. NSArray *indexPathes = [_tableView indexPathsForRowsInRect:rect];
  192. NSMutableArray * allIndexPath = [NSMutableArray arrayWithArray:indexPathes];
  193. [_dqSlcIndexPs removeAllObjects];
  194. for (NSIndexPath *indexPath in allIndexPath) {
  195. //使用代码方式选中一行
  196. if (indexPath.row == 0 && _level == 1) {
  197. [_dqSlcIndexPs addObject:indexPath];
  198. [_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
  199. }
  200. }
  201. }else{
  202. for (NSIndexPath *indexPath in _dqSlcIndexPs) {
  203. //使用代码方式取消选中
  204. [_tableView deselectRowAtIndexPath:indexPath animated:YES];
  205. }
  206. [_dqSlcIndexPs removeAllObjects];
  207. }
  208. }
  209. -(void)allQxClick:(UIButton *)button{
  210. button.selected = !button.selected;
  211. //展开所有分组
  212. NSNumber *number = @1;
  213. for (int i=0; i<_sectionStatus.count; i++) {
  214. [_sectionStatus replaceObjectAtIndex:i withObject:number];
  215. }
  216. [self.tableView reloadData];
  217. if (button.selected == YES) {
  218. // //获取表格视图内容的尺寸
  219. CGSize size = _tableView.contentSize;
  220. CGRect rect = CGRectMake(0, 0, size.width, size.height);
  221. //获取指定区域的cell的indexPath
  222. NSArray *indexPathes = [_tableView indexPathsForRowsInRect:rect];
  223. NSMutableArray * allIndexPath = [NSMutableArray arrayWithArray:indexPathes];
  224. _selectedIndexPathes = [allIndexPath mutableCopy];
  225. for (NSIndexPath *indexPath in allIndexPath) {
  226. if (indexPath.row == 0 && _level == 1) {
  227. [_selectedIndexPathes removeObject:indexPath];
  228. continue;
  229. }
  230. [_tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
  231. }
  232. }else{
  233. //关闭所有分组
  234. [self createStatusArray];
  235. [self.tableView reloadData];
  236. for (NSIndexPath *indexPath in _selectedIndexPathes) {
  237. //使用代码方式取消选中
  238. [_tableView deselectRowAtIndexPath:indexPath animated:YES];
  239. }
  240. [_selectedIndexPathes removeAllObjects];
  241. }
  242. }
  243. - (void)cancelClick:(UIButton *)sender {
  244. [_btnView removeFromSuperview];
  245. _btnView = nil;
  246. [self.tableView setEditing:NO];
  247. [_notifyBtn setTitle:@"选择\n地市" forState:UIControlStateNormal];
  248. for (NSIndexPath *indexPath in _selectedIndexPathes) {
  249. //使用代码方式取消选中一行
  250. [_tableView deselectRowAtIndexPath:indexPath animated:YES];
  251. }
  252. //清空选中cell的记录数组
  253. [_selectedIndexPathes removeAllObjects];
  254. }
  255. #pragma mark tableview代理相关
  256. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  257. return _qxArr.count;
  258. }
  259. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  260. if ([_sectionStatus[section] isEqualToNumber:@0]) {
  261. return 1;
  262. }
  263. return [_qxArr[section] count];
  264. }
  265. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  266. if (indexPath.row == 0 && _level == 1) {
  267. Notify_SelectCityCell * cell = [Notify_SelectCityCell cellForTableView:tableView];
  268. cell.title = _qxArr[indexPath.section][indexPath.row][@"name"];
  269. [cell updateWithStatus:[_sectionStatus[indexPath.section] boolValue]];
  270. if (self.tableView.editing && [_dqSlcIndexPs containsObject:indexPath]) {
  271. [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
  272. }
  273. cell.openBlock = ^{
  274. NSNumber *number = [_sectionStatus[indexPath.section] isEqualToNumber:@0] ? @1 : @0;
  275. //改变状态
  276. [_sectionStatus replaceObjectAtIndex:indexPath.section withObject:number];
  277. //刷新指定的组的数据
  278. [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
  279. };
  280. return cell;
  281. }
  282. SelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
  283. if (!cell) {
  284. cell = [[SelectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
  285. }
  286. cell.textLabel.text = _qxArr[indexPath.section][indexPath.row][@"name"];
  287. if (self.tableView.editing && [_selectedIndexPathes containsObject:indexPath]) {
  288. [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
  289. }
  290. return cell;
  291. }
  292. #pragma mark
  293. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  294. return 10;
  295. }
  296. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  297. return 0.1f;
  298. }
  299. #pragma mark
  300. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  301. return UITableViewCellEditingStyleInsert |UITableViewCellEditingStyleDelete;
  302. }
  303. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  304. if (!tableView.editing) {
  305. return;
  306. }
  307. if (indexPath.row == 0 && _level == 1) {
  308. if (![_dqSlcIndexPs containsObject:indexPath]) {
  309. [_dqSlcIndexPs addObject:indexPath];
  310. }
  311. return;
  312. }
  313. if (![_selectedIndexPathes containsObject:indexPath]) {
  314. [_selectedIndexPathes addObject:indexPath];
  315. }
  316. return;
  317. }
  318. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
  319. if (!tableView.editing) {
  320. return;
  321. }
  322. if (indexPath.row == 0 && _level == 1) {
  323. if ([_dqSlcIndexPs containsObject:indexPath]) {
  324. [_dqSlcIndexPs removeObject:indexPath];
  325. }
  326. return;
  327. }
  328. if ([_selectedIndexPathes containsObject:indexPath]) {
  329. [_selectedIndexPathes removeObject:indexPath];
  330. }
  331. }
  332. - (void)didReceiveMemoryWarning {
  333. [super didReceiveMemoryWarning];
  334. // Dispose of any resources that can be recreated.
  335. }
  336. /*
  337. #pragma mark - Navigation
  338. // In a storyboard-based application, you will often want to do a little preparation before navigation
  339. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  340. // Get the new view controller using [segue destinationViewController].
  341. // Pass the selected object to the new view controller.
  342. }
  343. */
  344. @end