Notify_SelectCityVC.m 14 KB

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