// // ManageDeveceList_VC.m // Haishenghai-master // // Created by GG on 2019/1/4. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved. // #import "ManageDeveceList_VC.h" #import "Header.h" #import "DeveceListCell.h" #import "BottomView.h" @interface ManageDeveceList_VC () @property(nonatomic,strong)NSMutableArray *dataArray; @property(nonatomic, strong) NSMutableArray *markArr;//标记数据的数组 @property(nonatomic, strong) NSMutableArray *deleteArray;//删除数据的数组 @property(nonatomic, strong) NSMutableArray *selectArray;//选中数据的数组 @property(nonatomic,strong)UIButton *btn; @property (nonatomic ,strong) BottomView *bottom_view;//底部视图 @property (nonatomic,assign)NSInteger selectedId; @property (assign, nonatomic) NSInteger deleteNum; @end @implementation ManageDeveceList_VC - (BottomView *)bottom_view{ if (!_bottom_view) { self.bottom_view = [[BottomView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, self.view.frame.size.width, 50)]; _bottom_view.backgroundColor = [UIColor yellowColor]; [_bottom_view.deleteBtn addTarget:self action:@selector(deleteData) forControlEvents:UIControlEventTouchUpInside]; [_bottom_view.allBtn addTarget:self action:@selector(tapAllBtn:) forControlEvents:UIControlEventTouchUpInside]; } return _bottom_view; } -(NSMutableArray *)dataArray{ if (!_dataArray) { self.dataArray = [NSMutableArray array]; } return _dataArray; } -(NSMutableArray *)selectArray{ if (!_selectArray) { self.selectArray = [NSMutableArray array]; } return _selectArray; } -(NSMutableArray *)deleteArray{ if (!_deleteArray) { self.deleteArray = [NSMutableArray array]; } return _deleteArray; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self setupUI]; NSString *userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"]; // [DataSourceManager getDeveceWithUrlUserID:userId Page:@"1" completionBlock:^(NSMutableArray *array) { // _dataArray =array; // [self.tableView reloadData]; // }]; } -(UIButton *)btn{ if (!_btn) { self.btn = [UIButton buttonWithType:UIButtonTypeCustom]; _btn.frame = CGRectMake(0, 0, 50, 44); [_btn setTitle:@"编辑" forState:UIControlStateNormal]; [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_btn addTarget:self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside]; } return _btn; } -(void)setupUI{ self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:60/255.0 green:114/255.0 blue:255/255.0 alpha:1]; [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}]; if (@available (iOS 11.0,*)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; }else{ self.automaticallyAdjustsScrollViewInsets = NO; } self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.btn]; // UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; // rightButton.frame = CGRectMake(0, 0, 22, 22); // [rightButton setImage:[UIImage imageNamed:@"hsh_user_detail"] forState:UIControlStateNormal]; // [rightButton addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside]; // UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton]; // // self.navigationItem.rightBarButtonItem = rightItem; _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, HEIGHT-64) style:UITableViewStylePlain]; // _tableView.backgroundColor = [UIColor grayColor]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.userInteractionEnabled = YES; _tableView.showsHorizontalScrollIndicator=NO; _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; [self.view addSubview:_tableView]; } #pragma mark-- tableviewDelegate -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _dataArray.count; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 150; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellId = @"cell"; DeveceListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId]; if (!cell) { cell = [[DeveceListCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId]; } //让单元格的分割线和Cell一样宽 cell.preservesSuperviewLayoutMargins = false; cell.separatorInset = UIEdgeInsetsZero; cell.layoutMargins = UIEdgeInsetsZero; deveceListModel *model = [self.dataArray objectAtIndex:indexPath.row]; cell.deveNumberLabel.text = model.deviceId; // cell.signalLabel.text = model. cell.deplementLabel.text = model.deviceName; cell.locationLabel.text = model.deviceLocation; cell.d_lationLabel.text = model.deviceLocation; _selectedId = indexPath.row; return cell; } -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } //选择编辑的方式,按照选择的方式对表进行处理 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { } } //选择你要对表进行处理的方式 默认是删除方式 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert; } //选中时将选中行的在self.dataArray 中的数据添加到删除数组self.deleteArr中 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ deveceListModel *model = self.dataArray [indexPath.row]; [_selectArray addObject:model.deviceId]; NSLog(@"%ld",_selectArray.count); [self.deleteArray addObject:[self.dataArray objectAtIndex:indexPath.row]]; } //取消选中时 将存放在self.deleteArr中的数据移除 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [self.deleteArray removeObject:[self.dataArray objectAtIndex:indexPath.row]]; } -(void)confirmBtnClick{ //编辑 //弹出视图 [popMenuView addPopTableViewSelectWithFrame:CGRectMake(0, 0, 120, 100) selectData:@[@"添加",@"删除"] images:@[@"hsh_home_add",@"hsh_login_delete"] action:^(NSInteger index) { //点击事件在这里 if (index==0) { AddDevece_VC *deveceVC = [[AddDevece_VC alloc]init]; [self.navigationController pushViewController:deveceVC animated:YES]; }else { //批量删除 // _deleteBtn.enabled = YES; //支持同时选中多行 self.tableView.allowsMultipleSelectionDuringEditing = YES; self.tableView.editing = !self.tableView.editing; // if (self.tableView.editing) { // //删除 // // [self.dataArray removeObjectsInArray:self.deleteArr]; // [self.tableView reloadData]; // // } // else return; } } animation:YES]; } #pragma mark----btn Click -(void)editBtnClick:(UIButton *)btn{ btn.selected = !btn.selected; if (btn.selected) { //点击编辑的时候清空删除数组 [self.deleteArray removeAllObjects]; [_btn setTitle:@"完成" forState:UIControlStateNormal]; // _isInsertEdit = YES;//这个时候是全选模式 [_tableView setEditing:YES animated:YES]; //如果在全选状态下,点击完成,再次进来的时候需要改变按钮的文字和点击状态 if (_bottom_view.allBtn.selected) { _bottom_view.allBtn.selected = !_bottom_view.allBtn.selected; [_bottom_view.allBtn setTitle:@"全选" forState:UIControlStateNormal]; } //添加底部视图 CGRect frame = self.bottom_view.frame; frame.origin.y -= 50; [UIView animateWithDuration:0.5 animations:^{ self.bottom_view.frame = frame; [self.view addSubview:self.bottom_view]; }]; }else{ [_btn setTitle:@"编辑" forState:UIControlStateNormal]; // _isInsertEdit = NO; [_tableView setEditing:NO animated:YES]; [UIView animateWithDuration:0.5 animations:^{ CGPoint point = self.bottom_view.center; point.y += 50; self.bottom_view.center = point; } completion:^(BOOL finished) { [self.bottom_view removeFromSuperview]; }]; }} -(void)deleteData{ if (self.tableView.editing) { [self upDeleteData]; if (self.dataArray.count >0 ) { //在这里发送请求删除选中的deveceID删除 [self.dataArray removeObjectsInArray:self.deleteArray]; [self.tableView reloadData]; // } } else return; } -(void)upDeleteData{ //创建JsonURL NSString *strUrl = @"https://www.hsh-iot.cn/YunGanKeJi/deletedevice"; //创建manager AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];//不设置会报-1016或者会有编码问题 // //申明返回的结果是json类型 manager.responseSerializer = [AFJSONResponseSerializer serializer]; // 申明请求的数据是json类型 manager.requestSerializer=[AFJSONRequestSerializer serializer]; [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain",nil]]; NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST"URLString:strUrl parameters:nil error:nil]; [request addValue:@"application/json"forHTTPHeaderField:@"Content-Type"]; //获取Token NSUserDefaults *tokenid = [NSUserDefaults standardUserDefaults]; NSString *token = [tokenid objectForKey:@"token"]; //将Token作为请求头上传 [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"]; NSMutableDictionary *param = [[NSMutableDictionary alloc]init]; NSString *string = @"34,37"; [param setValue:string forKey:@"id"]; [manager POST:strUrl parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"----测试数据%@",responseObject); if ([[responseObject objectForKey:@"message"]isEqualToString:@"ok"]) { }else{ NSString *str = [responseObject objectForKey:@"object"]; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; [alert show]; } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }]; } -(void)tapAllBtn:(UIButton *)btn{ btn.selected = !btn.selected; if (btn.selected) { for (int i = 0; i< self.dataArray.count; i++) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0]; //全选实现方法 [_tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop]; self.deleteNum = self.dataArray.count; } //点击全选的时候需要清除deleteArray里面的数据,防止deleteArray里面的数据和列表数据不一致 if (self.deleteArray.count >0) { [self.deleteArray removeAllObjects]; self.deleteNum =0; } [self.deleteArray addObjectsFromArray:self.dataArray]; [btn setTitle:@"取消" forState:UIControlStateNormal]; }else{ //取消选中 for (int i = 0; i< self.dataArray.count; i++) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0]; [_tableView deselectRowAtIndexPath:indexPath animated:NO]; } [btn setTitle:@"全选" forState:UIControlStateNormal]; [self.deleteArray removeAllObjects]; } } - (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