// // Notify_SelectSchVC.m // LNManager // // Created by EchoShacolee on 2017/6/21. // Copyright © 2017年 lee. All rights reserved. // #import "Notify_SelectSchVC.h" @interface Notify_SelectSchVC () { NSMutableArray *_selectedIndexPathes;//记录选中的cell // UIView * _btnView; UIButton *_notifyBtn; UIButton *_allBtn; UIButton *_cancelBtn; } @end @implementation Notify_SelectSchVC - (void)viewDidLoad { [super viewDidLoad]; //实例化 _selectedIndexPathes = [[NSMutableArray alloc] init]; [self getPermission]; } #pragma mark 重写这个方法,清除已经选中 -(void)setDataArr:(NSMutableArray *)dataArr{ [super setDataArr:dataArr]; //dataArr更新后需要清楚已选择的 for (NSIndexPath *indexPath in _selectedIndexPathes) { //使用代码方式取消选中一行 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; } [_selectedIndexPathes removeAllObjects]; } -(void)createSelectBtn{ _btnView = [[UIView alloc]initWithFrame:CGRectMake(kSize.width-180, kSize.height-120-95-SafeAreaBottomHeight, 180, 60)]; [self.view addSubview:_btnView]; [self.view insertSubview:_btnView belowSubview:self.holderV]; _allBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _allBtn.backgroundColor = [UIColor lightGrayColor]; _allBtn.frame = CGRectMake(0, 0, 80, 30); [_allBtn setTitle:@"全选" forState:UIControlStateNormal]; [_allBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_allBtn setTitleColor:COLOR_THEME forState:UIControlStateSelected]; [_allBtn addTarget:self action:@selector(allClick:) forControlEvents:UIControlEventTouchUpInside]; _allBtn.hidden = YES; [_btnView addSubview:_allBtn]; _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _cancelBtn.backgroundColor = [UIColor lightGrayColor]; _cancelBtn.frame = CGRectMake(0, 40, 80, 30); [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal]; [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside]; _cancelBtn.hidden = YES; [_btnView addSubview:_cancelBtn]; _notifyBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _notifyBtn.backgroundColor = COLOR_THEME; _notifyBtn.frame = CGRectMake(100, 5, 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]; [_btnView addSubview:_notifyBtn]; } -(void)allClick:(UIButton *)button{ button.selected = !button.selected; if (button.selected == YES) { //获取表格视图内容的尺寸 CGSize size = self.tableView.contentSize; CGRect rect = CGRectMake(0, 0, size.width, size.height); //获取指定区域的cell的indexPath NSArray *indexPathes = [self.tableView indexPathsForRowsInRect:rect]; NSMutableArray * allIndexPath = [NSMutableArray arrayWithArray:indexPathes]; _selectedIndexPathes = allIndexPath; for (NSIndexPath *indexPath in _selectedIndexPathes) { //使用代码方式选中一行 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; } }else{ for (NSIndexPath *indexPath in _selectedIndexPathes) { //使用代码方式取消选中 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; } [_selectedIndexPathes removeAllObjects]; } } - (void)cancelClick:(UIButton *)sender { [self.tableView setEditing:NO]; _allBtn.hidden = YES; _allBtn.selected = NO; _cancelBtn.hidden = YES; [_notifyBtn setTitle:@"选择\n驾校" forState:UIControlStateNormal]; //清空选中cell的记录数组 [_selectedIndexPathes removeAllObjects]; } -(void)notifiBtnClick:(UIButton *)sender{ [_notifyBtn setTitle:@"完成" forState:UIControlStateNormal]; if (self.tableView.editing == NO) { [self.tableView setEditing:YES]; _allBtn.hidden = NO; _cancelBtn.hidden = NO; return; } NSMutableArray * allId = [NSMutableArray new]; for (NSIndexPath *indexPath in _selectedIndexPathes) { [allId addObject:_dataSource[indexPath.section][indexPath.row][@"TSI_INSCODE"]]; } if (allId.count > 0) { [self sendNotifyWithIds:allId]; }else{ [self showMsgByAlertVCWithString:@"请选择驾校"]; } } -(void)sendNotifyWithIds:(NSMutableArray *)ids{ if (self.blcok) { self.blcok(ids); } } #pragma mark - table view delegate - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleInsert |UITableViewCellEditingStyleDelete; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (!_btnView) { return; } if (self.tableView.isEditing) { //当选中cell时,记录其indexPath if (![_selectedIndexPathes containsObject:indexPath]) { [_selectedIndexPathes addObject:indexPath]; } return; } [self showMsgByAlertVCWithString:@"点击右下角图标可选择驾校发送通知"]; } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { //当取消选中cell时,从记录的数组中删除其indexPath if ([_selectedIndexPathes containsObject:indexPath] && self.tableView.isEditing) { [_selectedIndexPathes removeObject:indexPath]; } } #pragma mark - 权限验证 - (void) getPermission{ [Tools permissionValidationWithID:@"751" result:^(BOOL isCan, NSString *failureStr) { if (isCan) { [self createSelectBtn]; } }]; } - (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