Notify_SelectSchVC.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 createSelectBtn];
  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, 180, 60)];
  38. [self.view addSubview:_btnView];
  39. _allBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  40. _allBtn.backgroundColor = [UIColor lightGrayColor];
  41. _allBtn.frame = CGRectMake(0, 0, 80, 30);
  42. [_allBtn setTitle:@"全选" forState:UIControlStateNormal];
  43. [_allBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  44. [_allBtn setTitleColor:COLOR_THEME forState:UIControlStateSelected];
  45. [_allBtn addTarget:self action:@selector(allClick:) forControlEvents:UIControlEventTouchUpInside];
  46. _allBtn.hidden = YES;
  47. [_btnView addSubview:_allBtn];
  48. _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  49. _cancelBtn.backgroundColor = [UIColor lightGrayColor];
  50. _cancelBtn.frame = CGRectMake(0, 40, 80, 30);
  51. [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
  52. [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  53. [_cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
  54. _cancelBtn.hidden = YES;
  55. [_btnView addSubview:_cancelBtn];
  56. _notifyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  57. _notifyBtn.backgroundColor = COLOR_THEME;
  58. _notifyBtn.frame = CGRectMake(100, 5, 60, 60);
  59. [_notifyBtn setTitle:@"选择\n驾校" forState:UIControlStateNormal];
  60. [_notifyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  61. _notifyBtn.titleLabel.numberOfLines = 0;
  62. _notifyBtn.layer.masksToBounds = YES;
  63. _notifyBtn.layer.cornerRadius = 30;
  64. [_notifyBtn addTarget:self action:@selector(notifiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  65. [_btnView addSubview:_notifyBtn];
  66. }
  67. -(void)allClick:(UIButton *)button{
  68. button.selected = !button.selected;
  69. if (button.selected == YES) {
  70. //获取表格视图内容的尺寸
  71. CGSize size = self.tableView.contentSize;
  72. CGRect rect = CGRectMake(0, 0, size.width, size.height);
  73. //获取指定区域的cell的indexPath
  74. NSArray *indexPathes = [self.tableView indexPathsForRowsInRect:rect];
  75. NSMutableArray * allIndexPath = [NSMutableArray arrayWithArray:indexPathes];
  76. _selectedIndexPathes = allIndexPath;
  77. for (NSIndexPath *indexPath in _selectedIndexPathes) {
  78. //使用代码方式选中一行
  79. [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
  80. }
  81. }else{
  82. for (NSIndexPath *indexPath in _selectedIndexPathes) {
  83. //使用代码方式取消选中
  84. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  85. }
  86. [_selectedIndexPathes removeAllObjects];
  87. }
  88. }
  89. - (void)cancelClick:(UIButton *)sender {
  90. [self.tableView setEditing:NO];
  91. _allBtn.hidden = YES;
  92. _allBtn.selected = NO;
  93. _cancelBtn.hidden = YES;
  94. [_notifyBtn setTitle:@"选择\n驾校" forState:UIControlStateNormal];
  95. //清空选中cell的记录数组
  96. [_selectedIndexPathes removeAllObjects];
  97. }
  98. -(void)notifiBtnClick:(UIButton *)sender{
  99. [_notifyBtn setTitle:@"完成" forState:UIControlStateNormal];
  100. if (self.tableView.editing == NO) {
  101. [self.tableView setEditing:YES];
  102. _allBtn.hidden = NO;
  103. _cancelBtn.hidden = NO;
  104. return;
  105. }
  106. NSMutableArray * allId = [NSMutableArray new];
  107. for (NSIndexPath *indexPath in _selectedIndexPathes) {
  108. [allId addObject:_dataSource[indexPath.section][indexPath.row][@"TSI_INSCODE"]];
  109. }
  110. if (allId.count > 0) {
  111. [self sendNotifyWithIds:allId];
  112. }else{
  113. [self showMsgByAlertVCWithString:@"请选择驾校"];
  114. }
  115. }
  116. -(void)sendNotifyWithIds:(NSMutableArray *)ids{
  117. if (self.blcok) {
  118. self.blcok(ids);
  119. }
  120. }
  121. #pragma mark - table view delegate
  122. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  123. return UITableViewCellEditingStyleInsert |UITableViewCellEditingStyleDelete;
  124. }
  125. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  126. if (self.tableView.isEditing) {
  127. //当选中cell时,记录其indexPath
  128. if (![_selectedIndexPathes containsObject:indexPath]) {
  129. [_selectedIndexPathes addObject:indexPath];
  130. }
  131. return;
  132. }
  133. [self showMsgByAlertVCWithString:@"点击右下角图标可选择驾校发送通知"];
  134. }
  135. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
  136. //当取消选中cell时,从记录的数组中删除其indexPath
  137. if ([_selectedIndexPathes containsObject:indexPath] && self.tableView.isEditing) {
  138. [_selectedIndexPathes removeObject:indexPath];
  139. }
  140. }
  141. - (void)didReceiveMemoryWarning {
  142. [super didReceiveMemoryWarning];
  143. // Dispose of any resources that can be recreated.
  144. }
  145. /*
  146. #pragma mark - Navigation
  147. // In a storyboard-based application, you will often want to do a little preparation before navigation
  148. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  149. // Get the new view controller using [segue destinationViewController].
  150. // Pass the selected object to the new view controller.
  151. }
  152. */
  153. @end