DeveceTableViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //
  2. // DeveceTableViewController.m
  3. // Haishenghai-master
  4. //
  5. // Created by GG on 2019/1/25.
  6. // Copyright © 2019年 Haishenghai intelligence network technology. All rights reserved.
  7. //
  8. #import "DeveceTableViewController.h"
  9. @interface DeveceTableViewController ()
  10. {
  11. NSString *siteLocation;
  12. UIImageView *blankImageView;
  13. UILabel *blankLabel;
  14. }
  15. /*数据源*/
  16. @property (nonatomic ,strong)NSMutableArray *dataArray;
  17. @property(nonatomic,strong)NSMutableArray *siteArray;
  18. @property (nonatomic,strong)UILabel *siteLabel;
  19. @end
  20. //static NSString *const cellID = @"cellID";
  21. @implementation DeveceTableViewController
  22. -(void)viewWillAppear:(BOOL)animated{
  23. }
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLocation:) name:@"location" object:nil];
  27. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  28. // [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID ];
  29. _dataArray = [NSMutableArray array];
  30. // PlaceModel *model = [_dataArray objectAtIndex:0];
  31. blankImageView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 100, 110, 110)];
  32. blankImageView.image = [UIImage imageNamed:@"hsh_equipment_lose"];
  33. [self.view addSubview:blankImageView];
  34. blankLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 210, 110, 40)];
  35. [self.view addSubview:blankLabel];
  36. blankLabel.textAlignment = NSTextAlignmentCenter;
  37. blankLabel.text = @"暂无设备";
  38. //获取默认的数据
  39. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeSiteID:) name:@"ChangeSiteID" object:nil];
  40. }
  41. -(void)changeSiteID:(NSNotification *)noti{
  42. NSDictionary *dict = noti.userInfo;
  43. NSArray *siteArr = [dict objectForKey:@"siteID"];
  44. NSString *site = [siteArr objectAtIndex:0];
  45. siteLocation = [siteArr objectAtIndex:1];
  46. [self.tableView reloadData];
  47. if ([site isEqualToString:@""]) {
  48. [_dataArray removeAllObjects];
  49. blankImageView.hidden =NO;
  50. blankLabel.hidden =NO;
  51. }else{
  52. [DataSourceManager getDeveceWithUrlPagesize:100 SiteId:site Page:1 completionBlock:^(NSMutableArray *array) {
  53. NSLog(@"%@",site);
  54. if (array.count ==0) {
  55. blankImageView.hidden =NO;
  56. blankLabel.hidden =NO;
  57. return ;
  58. }else{
  59. blankImageView.hidden =YES;
  60. blankLabel.hidden =YES;
  61. [_dataArray removeAllObjects];
  62. _dataArray = array;
  63. [self.tableView reloadData];
  64. }
  65. }];
  66. }
  67. }
  68. -(void)btnClick{
  69. NSLog(@"去添加设备");
  70. }
  71. -(void)changeLocation:(NSNotification *)noti{
  72. NSDictionary *dict = noti.userInfo;
  73. siteLocation = [dict objectForKey:@"location"];
  74. }
  75. - (void)siteViewContller:(SiteTableViewController *)controller didSelectRowWithData:(NSMutableArray *)data{
  76. [_dataArray removeAllObjects];
  77. _dataArray = data;
  78. if (_dataArray.count==0) {
  79. blankImageView.hidden =NO;
  80. blankLabel.hidden =NO;
  81. }else{
  82. blankImageView.hidden =YES;
  83. blankLabel.hidden =YES;
  84. }
  85. NSLog(@"这是传递过来的数据%@2",data);
  86. [self.tableView reloadData];
  87. }
  88. - (void)didReceiveMemoryWarning {
  89. [super didReceiveMemoryWarning];
  90. // Dispose of any resources that can be recreated.
  91. }
  92. #pragma mark - Table view data source
  93. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  94. return 1;
  95. }
  96. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  97. return _dataArray.count;
  98. }
  99. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  100. return 60;
  101. }
  102. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  103. return 60;
  104. }
  105. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  106. static NSString *CellIdentifier = @"Cell";
  107. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  108. if (!cell) {
  109. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  110. }
  111. NSLog(@"~~~~~~~%@",self.dataArray);
  112. deveceListModel *model = self.dataArray[indexPath.row];
  113. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  114. UILabel *numberlab =[[UILabel alloc]init];
  115. numberlab.frame = CGRectMake(10, 5, self.view.frame.size.width-80, 25);
  116. numberlab.font = [UIFont systemFontOfSize:14];
  117. numberlab.textAlignment = NSTextAlignmentLeft;
  118. [cell.contentView addSubview:numberlab];
  119. numberlab.text = [NSString stringWithFormat:@"编号:%@",model.deviceId];
  120. UILabel *locationlab =[[UILabel alloc]init];
  121. locationlab.frame = CGRectMake(10, 30, self.view.frame.size.width-80, 25);
  122. locationlab.font = [UIFont systemFontOfSize:14];
  123. locationlab.textAlignment = NSTextAlignmentLeft;
  124. [cell.contentView addSubview:locationlab];
  125. locationlab.text = [NSString stringWithFormat:@"位置:%@",model.deviceLocation];
  126. UILabel *statelab =[[UILabel alloc]init];
  127. statelab.frame = CGRectMake(self.view.frame.size.width-80, 7, 80, 44);
  128. [cell.contentView addSubview:statelab];
  129. statelab.font = [UIFont systemFontOfSize:18];
  130. statelab.textAlignment = NSTextAlignmentCenter;
  131. if ([model.troubleType isEqualToString:@"1"]) {
  132. statelab.text = @"火警";
  133. statelab.textColor = [UIColor redColor];
  134. }else if([model.troubleType isEqualToString:@"0"]){
  135. statelab.text = @"故障";
  136. statelab.textColor = [UIColor orangeColor];
  137. }else{
  138. statelab.text = @"正常";
  139. statelab.textColor = [UIColor colorWithRed:78/255.0 green:105/255.0 blue:255/255.0 alpha:1];
  140. }
  141. return cell;
  142. }
  143. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  144. UIView *headerView = [[UIView alloc]init];
  145. headerView.backgroundColor = [UIColor whiteColor];
  146. _siteLabel = [[UILabel alloc]init];
  147. _siteLabel.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 40);
  148. _siteLabel.font = [UIFont systemFontOfSize:14];
  149. _siteLabel.textColor = [UIColor blackColor];
  150. _siteLabel.textAlignment = NSTextAlignmentCenter;
  151. _siteLabel.text = siteLocation;
  152. _siteLabel.numberOfLines = 0;
  153. _siteLabel.lineBreakMode =NSLineBreakByWordWrapping;
  154. [headerView addSubview:_siteLabel];
  155. UILabel *deviceCountLabel = [[UILabel alloc]init];
  156. deviceCountLabel.frame = CGRectMake(0, 40, self.tableView.frame.size.width, 22);
  157. deviceCountLabel.font = [UIFont systemFontOfSize:14];
  158. deviceCountLabel.textColor = [UIColor blackColor];
  159. deviceCountLabel.textAlignment = NSTextAlignmentCenter;
  160. NSInteger count = _dataArray.count;
  161. deviceCountLabel.text = [NSString stringWithFormat: @"该场所设备总数量:%ld",(long)count];
  162. [headerView addSubview:deviceCountLabel];
  163. UIView *line = [[UIView alloc]init];
  164. line.frame = CGRectMake(0, 61.5, self.tableView.frame.size.width, 0.5);
  165. line.backgroundColor = [UIColor grayColor];
  166. [headerView addSubview:line];
  167. return headerView;
  168. }
  169. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  170. deveceListModel *list =[self.dataArray objectAtIndex:indexPath.row];
  171. DeveceDetail_VC *deve = [[DeveceDetail_VC alloc]init];
  172. deve.deviceName = list.deviceName;
  173. // deve.location = list.lo;
  174. deve.deveceID =list.deviceId;
  175. deve.d_location = list.deviceLocation;
  176. deve.hidesBottomBarWhenPushed =YES;
  177. [self.navigationController pushViewController:deve animated:YES];}
  178. #pragma mark ---->左滑删除和详情
  179. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  180. return YES;
  181. }
  182. - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED{
  183. // 添加一个删除按钮
  184. UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  185. //在这里添加点击事件
  186. // 删除数据源
  187. deveceListModel *list =[self.dataArray objectAtIndex:indexPath.row];
  188. NSString *deveceID = list.deviceId;
  189. //在这里添加点击事件
  190. [DataSourceManager deleteDeveceWithUrlDeveceId:deveceID completionBlock:^(NSDictionary *dic) {
  191. // 删除数据源
  192. [self.dataArray removeObjectAtIndex:indexPath.row];
  193. [self.tableView reloadData];
  194. //或者cell删除
  195. // [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  196. }];
  197. }];
  198. // 添加一个编辑按钮
  199. UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"详情"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  200. deveceListModel *list =[self.dataArray objectAtIndex:indexPath.row];
  201. DeveceDetail_VC *deve = [[DeveceDetail_VC alloc]init];
  202. deve.deviceName = list.deviceName;
  203. // deve.location = list.lo;
  204. deve.deveceID =list.deviceId;
  205. deve.d_location = list.deviceLocation;
  206. deve.hidesBottomBarWhenPushed =YES;
  207. [self.navigationController pushViewController:deve animated:YES];
  208. }];
  209. topRowAction.backgroundColor = [UIColor colorWithRed:78/255.0 green:105/255.0 blue:255/255.0 alpha:1];
  210. // 将设置好的按钮放到数组中返回
  211. return @[deleteRowAction, topRowAction];
  212. }
  213. - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
  214. NSLog(@"已经结束编辑cell");
  215. }
  216. //移除通知
  217. -(void)dealloc{
  218. //第二种方法.这里可以移除该控制器下名称为tongzhi的通知
  219. //移除名称为tongzhi的那个通知
  220. NSLog(@"移除了名称为tongzhi的通知");
  221. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"location" object:nil];
  222. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ChangeSiteID" object:nil];
  223. }
  224. @end