ManageDeveceList_VC.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //
  2. // ManageDeveceList_VC.m
  3. // Haishenghai-master
  4. //
  5. // Created by GG on 2019/1/4.
  6. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved.
  7. //
  8. #import "ManageDeveceList_VC.h"
  9. #import "Header.h"
  10. #import "DeveceListCell.h"
  11. #import "BottomView.h"
  12. @interface ManageDeveceList_VC ()<UITableViewDelegate,UITableViewDataSource>
  13. @property(nonatomic,strong)NSMutableArray *dataArray;
  14. @property(nonatomic, strong) NSMutableArray *markArr;//标记数据的数组
  15. @property(nonatomic, strong) NSMutableArray *deleteArray;//删除数据的数组
  16. @property(nonatomic, strong) NSMutableArray *selectArray;//选中数据的数组
  17. @property(nonatomic,strong)UIButton *btn;
  18. @property (nonatomic ,strong) BottomView *bottom_view;//底部视图
  19. @property (nonatomic,assign)NSInteger selectedId;
  20. @property (assign, nonatomic) NSInteger deleteNum;
  21. @end
  22. @implementation ManageDeveceList_VC
  23. - (BottomView *)bottom_view{
  24. if (!_bottom_view) {
  25. self.bottom_view = [[BottomView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, self.view.frame.size.width, 50)];
  26. _bottom_view.backgroundColor = [UIColor yellowColor];
  27. [_bottom_view.deleteBtn addTarget:self action:@selector(deleteData) forControlEvents:UIControlEventTouchUpInside];
  28. [_bottom_view.allBtn addTarget:self action:@selector(tapAllBtn:) forControlEvents:UIControlEventTouchUpInside];
  29. }
  30. return _bottom_view;
  31. }
  32. -(NSMutableArray *)dataArray{
  33. if (!_dataArray) {
  34. self.dataArray = [NSMutableArray array];
  35. }
  36. return _dataArray;
  37. }
  38. -(NSMutableArray *)selectArray{
  39. if (!_selectArray) {
  40. self.selectArray = [NSMutableArray array];
  41. }
  42. return _selectArray;
  43. }
  44. -(NSMutableArray *)deleteArray{
  45. if (!_deleteArray) {
  46. self.deleteArray = [NSMutableArray array];
  47. }
  48. return _deleteArray;
  49. }
  50. - (void)viewDidLoad {
  51. [super viewDidLoad];
  52. self.view.backgroundColor = [UIColor whiteColor];
  53. [self setupUI];
  54. NSString *userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
  55. // [DataSourceManager getDeveceWithUrlUserID:userId Page:@"1" completionBlock:^(NSMutableArray *array) {
  56. // _dataArray =array;
  57. // [self.tableView reloadData];
  58. // }];
  59. }
  60. -(UIButton *)btn{
  61. if (!_btn) {
  62. self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
  63. _btn.frame = CGRectMake(0, 0, 50, 44);
  64. [_btn setTitle:@"编辑" forState:UIControlStateNormal];
  65. [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  66. [_btn addTarget:self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  67. }
  68. return _btn;
  69. }
  70. -(void)setupUI{
  71. self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:60/255.0 green:114/255.0 blue:255/255.0 alpha:1];
  72. [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
  73. if (@available (iOS 11.0,*)) {
  74. self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  75. }else{
  76. self.automaticallyAdjustsScrollViewInsets = NO;
  77. }
  78. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.btn];
  79. // UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
  80. // rightButton.frame = CGRectMake(0, 0, 22, 22);
  81. // [rightButton setImage:[UIImage imageNamed:@"hsh_user_detail"] forState:UIControlStateNormal];
  82. // [rightButton addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
  83. // UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
  84. //
  85. // self.navigationItem.rightBarButtonItem = rightItem;
  86. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, HEIGHT-64) style:UITableViewStylePlain];
  87. // _tableView.backgroundColor = [UIColor grayColor];
  88. _tableView.delegate = self;
  89. _tableView.dataSource = self;
  90. _tableView.userInteractionEnabled = YES;
  91. _tableView.showsHorizontalScrollIndicator=NO;
  92. _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  93. [self.view addSubview:_tableView];
  94. }
  95. #pragma mark-- tableviewDelegate
  96. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  97. return _dataArray.count;
  98. }
  99. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  100. return 150;
  101. }
  102. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  103. static NSString *cellId = @"cell";
  104. DeveceListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  105. if (!cell) {
  106. cell = [[DeveceListCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
  107. }
  108. //让单元格的分割线和Cell一样宽
  109. cell.preservesSuperviewLayoutMargins = false;
  110. cell.separatorInset = UIEdgeInsetsZero;
  111. cell.layoutMargins = UIEdgeInsetsZero;
  112. deveceListModel *model = [self.dataArray objectAtIndex:indexPath.row];
  113. cell.deveNumberLabel.text = model.deviceId;
  114. // cell.signalLabel.text = model.
  115. cell.deplementLabel.text = model.deviceName;
  116. cell.locationLabel.text = model.deviceLocation;
  117. cell.d_lationLabel.text = model.deviceLocation;
  118. _selectedId = indexPath.row;
  119. return cell;
  120. }
  121. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  122. return YES;
  123. }
  124. //选择编辑的方式,按照选择的方式对表进行处理
  125. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
  126. if (editingStyle == UITableViewCellEditingStyleDelete) {
  127. }
  128. }
  129. //选择你要对表进行处理的方式 默认是删除方式
  130. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  131. return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
  132. }
  133. //选中时将选中行的在self.dataArray 中的数据添加到删除数组self.deleteArr中
  134. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  135. deveceListModel *model = self.dataArray [indexPath.row];
  136. [_selectArray addObject:model.deviceId];
  137. NSLog(@"%ld",_selectArray.count);
  138. [self.deleteArray addObject:[self.dataArray objectAtIndex:indexPath.row]];
  139. }
  140. //取消选中时 将存放在self.deleteArr中的数据移除
  141. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
  142. [self.deleteArray removeObject:[self.dataArray objectAtIndex:indexPath.row]];
  143. }
  144. -(void)confirmBtnClick{
  145. //编辑
  146. //弹出视图
  147. [popMenuView addPopTableViewSelectWithFrame:CGRectMake(0, 0, 120, 100) selectData:@[@"添加",@"删除"] images:@[@"hsh_home_add",@"hsh_login_delete"] action:^(NSInteger index) {
  148. //点击事件在这里
  149. if (index==0) {
  150. AddDevece_VC *deveceVC = [[AddDevece_VC alloc]init];
  151. [self.navigationController pushViewController:deveceVC animated:YES];
  152. }else {
  153. //批量删除
  154. // _deleteBtn.enabled = YES;
  155. //支持同时选中多行
  156. self.tableView.allowsMultipleSelectionDuringEditing = YES;
  157. self.tableView.editing = !self.tableView.editing;
  158. // if (self.tableView.editing) {
  159. // //删除
  160. //
  161. // [self.dataArray removeObjectsInArray:self.deleteArr];
  162. // [self.tableView reloadData];
  163. //
  164. // }
  165. // else return;
  166. }
  167. } animation:YES];
  168. }
  169. #pragma mark----btn Click
  170. -(void)editBtnClick:(UIButton *)btn{
  171. btn.selected = !btn.selected;
  172. if (btn.selected) {
  173. //点击编辑的时候清空删除数组
  174. [self.deleteArray removeAllObjects];
  175. [_btn setTitle:@"完成" forState:UIControlStateNormal];
  176. // _isInsertEdit = YES;//这个时候是全选模式
  177. [_tableView setEditing:YES animated:YES];
  178. //如果在全选状态下,点击完成,再次进来的时候需要改变按钮的文字和点击状态
  179. if (_bottom_view.allBtn.selected) {
  180. _bottom_view.allBtn.selected = !_bottom_view.allBtn.selected;
  181. [_bottom_view.allBtn setTitle:@"全选" forState:UIControlStateNormal];
  182. }
  183. //添加底部视图
  184. CGRect frame = self.bottom_view.frame;
  185. frame.origin.y -= 50;
  186. [UIView animateWithDuration:0.5 animations:^{
  187. self.bottom_view.frame = frame;
  188. [self.view addSubview:self.bottom_view];
  189. }];
  190. }else{
  191. [_btn setTitle:@"编辑" forState:UIControlStateNormal];
  192. // _isInsertEdit = NO;
  193. [_tableView setEditing:NO animated:YES];
  194. [UIView animateWithDuration:0.5 animations:^{
  195. CGPoint point = self.bottom_view.center;
  196. point.y += 50;
  197. self.bottom_view.center = point;
  198. } completion:^(BOOL finished) {
  199. [self.bottom_view removeFromSuperview];
  200. }];
  201. }}
  202. -(void)deleteData{
  203. if (self.tableView.editing) {
  204. [self upDeleteData];
  205. if (self.dataArray.count >0 ) {
  206. //在这里发送请求删除选中的deveceID删除
  207. [self.dataArray removeObjectsInArray:self.deleteArray];
  208. [self.tableView reloadData];
  209. //
  210. }
  211. }
  212. else return;
  213. }
  214. -(void)upDeleteData{
  215. //创建JsonURL
  216. NSString *strUrl = @"https://www.hsh-iot.cn/YunGanKeJi/deletedevice";
  217. //创建manager
  218. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  219. manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];//不设置会报-1016或者会有编码问题
  220. // //申明返回的结果是json类型
  221. manager.responseSerializer = [AFJSONResponseSerializer serializer];
  222. // 申明请求的数据是json类型
  223. manager.requestSerializer=[AFJSONRequestSerializer serializer];
  224. [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain",nil]];
  225. NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST"URLString:strUrl parameters:nil error:nil];
  226. [request addValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
  227. //获取Token
  228. NSUserDefaults *tokenid = [NSUserDefaults standardUserDefaults];
  229. NSString *token = [tokenid objectForKey:@"token"];
  230. //将Token作为请求头上传
  231. [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
  232. NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
  233. NSString *string = @"34,37";
  234. [param setValue:string forKey:@"id"];
  235. [manager POST:strUrl parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  236. NSLog(@"----测试数据%@",responseObject);
  237. if ([[responseObject objectForKey:@"message"]isEqualToString:@"ok"]) {
  238. }else{
  239. NSString *str = [responseObject objectForKey:@"object"];
  240. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
  241. [alert show];
  242. }
  243. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  244. }];
  245. }
  246. -(void)tapAllBtn:(UIButton *)btn{
  247. btn.selected = !btn.selected;
  248. if (btn.selected) {
  249. for (int i = 0; i< self.dataArray.count; i++) {
  250. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  251. //全选实现方法
  252. [_tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
  253. self.deleteNum = self.dataArray.count;
  254. }
  255. //点击全选的时候需要清除deleteArray里面的数据,防止deleteArray里面的数据和列表数据不一致
  256. if (self.deleteArray.count >0) {
  257. [self.deleteArray removeAllObjects];
  258. self.deleteNum =0;
  259. }
  260. [self.deleteArray addObjectsFromArray:self.dataArray];
  261. [btn setTitle:@"取消" forState:UIControlStateNormal];
  262. }else{
  263. //取消选中
  264. for (int i = 0; i< self.dataArray.count; i++) {
  265. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  266. [_tableView deselectRowAtIndexPath:indexPath animated:NO];
  267. }
  268. [btn setTitle:@"全选" forState:UIControlStateNormal];
  269. [self.deleteArray removeAllObjects];
  270. }
  271. }
  272. - (void)didReceiveMemoryWarning {
  273. [super didReceiveMemoryWarning];
  274. // Dispose of any resources that can be recreated.
  275. }
  276. /*
  277. #pragma mark - Navigation
  278. // In a storyboard-based application, you will often want to do a little preparation before navigation
  279. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  280. // Get the new view controller using [segue destinationViewController].
  281. // Pass the selected object to the new view controller.
  282. }
  283. */
  284. @end