Notify_SelectSchVC.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // Notify_SelectSchVC.m
  3. // LNManager
  4. //
  5. // Created by EchoShacolee on 2017/6/21.
  6. // Copyright © 2017年 lee. All rights reserved.
  7. //
  8. #import "Notify_SelectSchVC.h"
  9. @interface Notify_SelectSchVC ()
  10. {
  11. NSMutableArray *_selectedIndexPathes;//记录选中的cell
  12. //
  13. UIView * _btnView;
  14. UIButton *_notifyBtn;
  15. UIButton *_allBtn;
  16. UIButton *_cancelBtn;
  17. }
  18. @end
  19. @implementation Notify_SelectSchVC
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. //实例化
  23. _selectedIndexPathes = [[NSMutableArray alloc] init];
  24. [self getPermission];
  25. }
  26. #pragma mark 重写这个方法,清除已经选中
  27. -(void)setDataArr:(NSMutableArray *)dataArr{
  28. [super setDataArr:dataArr];
  29. //dataArr更新后需要清楚已选择的
  30. for (NSIndexPath *indexPath in _selectedIndexPathes) {
  31. //使用代码方式取消选中一行
  32. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  33. }
  34. [_selectedIndexPathes removeAllObjects];
  35. }
  36. -(void)createSelectBtn{
  37. _btnView = [[UIView alloc]initWithFrame:CGRectMake(kSize.width-180, kSize.height-120-95-SafeAreaBottomHeight, 180, 60)];
  38. [self.view addSubview:_btnView];
  39. [self.view insertSubview:_btnView belowSubview:self.holderV];
  40. _allBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  41. _allBtn.backgroundColor = [UIColor lightGrayColor];
  42. _allBtn.frame = CGRectMake(0, 0, 80, 30);
  43. [_allBtn setTitle:@"全选" forState:UIControlStateNormal];
  44. [_allBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  45. [_allBtn setTitleColor:COLOR_THEME forState:UIControlStateSelected];
  46. [_allBtn addTarget:self action:@selector(allClick:) forControlEvents:UIControlEventTouchUpInside];
  47. _allBtn.hidden = YES;
  48. [_btnView addSubview:_allBtn];
  49. _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  50. _cancelBtn.backgroundColor = [UIColor lightGrayColor];
  51. _cancelBtn.frame = CGRectMake(0, 40, 80, 30);
  52. [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
  53. [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  54. [_cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
  55. _cancelBtn.hidden = YES;
  56. [_btnView addSubview:_cancelBtn];
  57. _notifyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  58. _notifyBtn.backgroundColor = COLOR_THEME;
  59. _notifyBtn.frame = CGRectMake(100, 5, 60, 60);
  60. [_notifyBtn setTitle:@"选择\n驾校" forState:UIControlStateNormal];
  61. [_notifyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  62. _notifyBtn.titleLabel.numberOfLines = 0;
  63. _notifyBtn.layer.masksToBounds = YES;
  64. _notifyBtn.layer.cornerRadius = 30;
  65. [_notifyBtn addTarget:self action:@selector(notifiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  66. [_btnView addSubview:_notifyBtn];
  67. }
  68. -(void)allClick:(UIButton *)button{
  69. button.selected = !button.selected;
  70. if (button.selected == YES) {
  71. //获取表格视图内容的尺寸
  72. CGSize size = self.tableView.contentSize;
  73. CGRect rect = CGRectMake(0, 0, size.width, size.height);
  74. //获取指定区域的cell的indexPath
  75. NSArray *indexPathes = [self.tableView indexPathsForRowsInRect:rect];
  76. NSMutableArray * allIndexPath = [NSMutableArray arrayWithArray:indexPathes];
  77. _selectedIndexPathes = allIndexPath;
  78. for (NSIndexPath *indexPath in _selectedIndexPathes) {
  79. //使用代码方式选中一行
  80. [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
  81. }
  82. }else{
  83. for (NSIndexPath *indexPath in _selectedIndexPathes) {
  84. //使用代码方式取消选中
  85. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  86. }
  87. [_selectedIndexPathes removeAllObjects];
  88. }
  89. }
  90. - (void)cancelClick:(UIButton *)sender {
  91. [self.tableView setEditing:NO];
  92. _allBtn.hidden = YES;
  93. _allBtn.selected = NO;
  94. _cancelBtn.hidden = YES;
  95. [_notifyBtn setTitle:@"选择\n驾校" forState:UIControlStateNormal];
  96. //清空选中cell的记录数组
  97. [_selectedIndexPathes removeAllObjects];
  98. }
  99. -(void)notifiBtnClick:(UIButton *)sender{
  100. [_notifyBtn setTitle:@"完成" forState:UIControlStateNormal];
  101. if (self.tableView.editing == NO) {
  102. [self.tableView setEditing:YES];
  103. _allBtn.hidden = NO;
  104. _cancelBtn.hidden = NO;
  105. return;
  106. }
  107. NSMutableArray * allId = [NSMutableArray new];
  108. for (NSIndexPath *indexPath in _selectedIndexPathes) {
  109. [allId addObject:_dataSource[indexPath.section][indexPath.row][@"TSI_INSCODE"]];
  110. }
  111. if (allId.count > 0) {
  112. [self sendNotifyWithIds:allId];
  113. }else{
  114. [self showMsgByAlertVCWithString:@"请选择驾校"];
  115. }
  116. }
  117. -(void)sendNotifyWithIds:(NSMutableArray *)ids{
  118. if (self.blcok) {
  119. self.blcok(ids);
  120. }
  121. }
  122. #pragma mark - table view delegate
  123. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  124. return UITableViewCellEditingStyleInsert |UITableViewCellEditingStyleDelete;
  125. }
  126. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  127. if (!_btnView) {
  128. return;
  129. }
  130. if (self.tableView.isEditing) {
  131. //当选中cell时,记录其indexPath
  132. if (![_selectedIndexPathes containsObject:indexPath]) {
  133. [_selectedIndexPathes addObject:indexPath];
  134. }
  135. return;
  136. }
  137. [self showMsgByAlertVCWithString:@"点击右下角图标可选择驾校发送通知"];
  138. }
  139. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
  140. //当取消选中cell时,从记录的数组中删除其indexPath
  141. if ([_selectedIndexPathes containsObject:indexPath] && self.tableView.isEditing) {
  142. [_selectedIndexPathes removeObject:indexPath];
  143. }
  144. }
  145. #pragma mark - 权限验证
  146. - (void) getPermission{
  147. [Tools permissionValidationWithID:@"751" result:^(BOOL isCan, NSString *failureStr) {
  148. if (isCan) {
  149. [self createSelectBtn];
  150. }
  151. }];
  152. }
  153. - (void)didReceiveMemoryWarning {
  154. [super didReceiveMemoryWarning];
  155. // Dispose of any resources that can be recreated.
  156. }
  157. /*
  158. #pragma mark - Navigation
  159. // In a storyboard-based application, you will often want to do a little preparation before navigation
  160. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  161. // Get the new view controller using [segue destinationViewController].
  162. // Pass the selected object to the new view controller.
  163. }
  164. */
  165. @end