// // DeveceTableViewController.m // Haishenghai-master // // Created by GG on 2019/1/25. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved. // #import "DeveceTableViewController.h" @interface DeveceTableViewController () { NSString *siteLocation; UIImageView *blankImageView; UILabel *blankLabel; } /*数据源*/ @property (nonatomic ,strong)NSMutableArray *dataArray; @property(nonatomic,strong)NSMutableArray *siteArray; @property (nonatomic,strong)UILabel *siteLabel; @end //static NSString *const cellID = @"cellID"; @implementation DeveceTableViewController -(void)viewWillAppear:(BOOL)animated{ } - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLocation:) name:@"location" object:nil]; self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; // [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID ]; _dataArray = [NSMutableArray array]; // PlaceModel *model = [_dataArray objectAtIndex:0]; blankImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 100, 110, 110)]; blankImageView.image = [UIImage imageNamed:@"hsh_equipment_lose"]; [self.view addSubview:blankImageView]; blankLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 210, 110, 40)]; [self.view addSubview:blankLabel]; blankLabel.textAlignment = NSTextAlignmentCenter; blankLabel.text = @"暂无设备"; //获取默认的数据 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeSiteID:) name:@"ChangeSiteID" object:nil]; } -(void)changeSiteID:(NSNotification *)noti{ NSDictionary *dict = noti.userInfo; NSArray *siteArr = [dict objectForKey:@"siteID"]; NSString *site = [siteArr objectAtIndex:0]; siteLocation = [siteArr objectAtIndex:1]; [self.tableView reloadData]; if ([site isEqualToString:@""]) { [_dataArray removeAllObjects]; blankImageView.hidden =NO; blankLabel.hidden =NO; }else{ [DataSourceManager getDeveceWithUrlPagesize:100 SiteId:site Page:1 completionBlock:^(NSMutableArray *array) { NSLog(@"%@",site); if (array.count ==0) { blankImageView.hidden =NO; blankLabel.hidden =NO; return ; }else{ blankImageView.hidden =YES; blankLabel.hidden =YES; [_dataArray removeAllObjects]; _dataArray = array; [self.tableView reloadData]; } }]; } } -(void)btnClick{ NSLog(@"去添加设备"); } -(void)changeLocation:(NSNotification *)noti{ NSDictionary *dict = noti.userInfo; siteLocation = [dict objectForKey:@"location"]; } - (void)siteViewContller:(SiteTableViewController *)controller didSelectRowWithData:(NSMutableArray *)data{ [_dataArray removeAllObjects]; _dataArray = data; if (_dataArray.count==0) { blankImageView.hidden =NO; blankLabel.hidden =NO; }else{ blankImageView.hidden =YES; blankLabel.hidden =YES; } NSLog(@"这是传递过来的数据%@2",data); [self.tableView reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataArray.count; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 60; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 60; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } NSLog(@"~~~~~~~%@",self.dataArray); deveceListModel *model = self.dataArray[indexPath.row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; UILabel *numberlab =[[UILabel alloc]init]; numberlab.frame = CGRectMake(10, 5, self.view.frame.size.width-80, 25); numberlab.font = [UIFont systemFontOfSize:14]; numberlab.textAlignment = NSTextAlignmentLeft; [cell.contentView addSubview:numberlab]; numberlab.text = [NSString stringWithFormat:@"编号:%@",model.deviceId]; UILabel *locationlab =[[UILabel alloc]init]; locationlab.frame = CGRectMake(10, 30, self.view.frame.size.width-80, 25); locationlab.font = [UIFont systemFontOfSize:14]; locationlab.textAlignment = NSTextAlignmentLeft; [cell.contentView addSubview:locationlab]; locationlab.text = [NSString stringWithFormat:@"位置:%@",model.deviceLocation]; UILabel *statelab =[[UILabel alloc]init]; statelab.frame = CGRectMake(self.view.frame.size.width-80, 7, 80, 44); [cell.contentView addSubview:statelab]; statelab.font = [UIFont systemFontOfSize:18]; statelab.textAlignment = NSTextAlignmentCenter; if ([model.troubleType isEqualToString:@"1"]) { statelab.text = @"火警"; statelab.textColor = [UIColor redColor]; }else if([model.troubleType isEqualToString:@"0"]){ statelab.text = @"故障"; statelab.textColor = [UIColor orangeColor]; }else{ statelab.text = @"正常"; statelab.textColor = [UIColor colorWithRed:78/255.0 green:105/255.0 blue:255/255.0 alpha:1]; } return cell; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *headerView = [[UIView alloc]init]; headerView.backgroundColor = [UIColor whiteColor]; _siteLabel = [[UILabel alloc]init]; _siteLabel.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 40); _siteLabel.font = [UIFont systemFontOfSize:14]; _siteLabel.textColor = [UIColor blackColor]; _siteLabel.textAlignment = NSTextAlignmentCenter; _siteLabel.text = siteLocation; _siteLabel.numberOfLines = 0; _siteLabel.lineBreakMode =NSLineBreakByWordWrapping; [headerView addSubview:_siteLabel]; UILabel *deviceCountLabel = [[UILabel alloc]init]; deviceCountLabel.frame = CGRectMake(0, 40, self.tableView.frame.size.width, 22); deviceCountLabel.font = [UIFont systemFontOfSize:14]; deviceCountLabel.textColor = [UIColor blackColor]; deviceCountLabel.textAlignment = NSTextAlignmentCenter; NSInteger count = _dataArray.count; deviceCountLabel.text = [NSString stringWithFormat: @"该场所设备总数量:%ld",(long)count]; [headerView addSubview:deviceCountLabel]; UIView *line = [[UIView alloc]init]; line.frame = CGRectMake(0, 61.5, self.tableView.frame.size.width, 0.5); line.backgroundColor = [UIColor grayColor]; [headerView addSubview:line]; return headerView; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ deveceListModel *list =[self.dataArray objectAtIndex:indexPath.row]; DeveceDetail_VC *deve = [[DeveceDetail_VC alloc]init]; deve.deviceName = list.deviceName; // deve.location = list.lo; deve.deveceID =list.deviceId; deve.d_location = list.deviceLocation; deve.hidesBottomBarWhenPushed =YES; [self.navigationController pushViewController:deve animated:YES];} #pragma mark ---->左滑删除和详情 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } - (nullable NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED{ // 添加一个删除按钮 UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { //在这里添加点击事件 // 删除数据源 deveceListModel *list =[self.dataArray objectAtIndex:indexPath.row]; NSString *deveceID = list.deviceId; //在这里添加点击事件 [DataSourceManager deleteDeveceWithUrlDeveceId:deveceID completionBlock:^(NSDictionary *dic) { // 删除数据源 [self.dataArray removeObjectAtIndex:indexPath.row]; [self.tableView reloadData]; //或者cell删除 // [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }]; }]; // 添加一个编辑按钮 UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"详情"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { deveceListModel *list =[self.dataArray objectAtIndex:indexPath.row]; DeveceDetail_VC *deve = [[DeveceDetail_VC alloc]init]; deve.deviceName = list.deviceName; // deve.location = list.lo; deve.deveceID =list.deviceId; deve.d_location = list.deviceLocation; deve.hidesBottomBarWhenPushed =YES; [self.navigationController pushViewController:deve animated:YES]; }]; topRowAction.backgroundColor = [UIColor colorWithRed:78/255.0 green:105/255.0 blue:255/255.0 alpha:1]; // 将设置好的按钮放到数组中返回 return @[deleteRowAction, topRowAction]; } - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"已经结束编辑cell"); } //移除通知 -(void)dealloc{ //第二种方法.这里可以移除该控制器下名称为tongzhi的通知 //移除名称为tongzhi的那个通知 NSLog(@"移除了名称为tongzhi的通知"); [[NSNotificationCenter defaultCenter] removeObserver:self name:@"location" object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeSiteID" object:nil]; } @end