ViewController.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. //
  2. // ViewController.m
  3. // Haishenghai-master
  4. //
  5. // Created by GG on 2018/12/29.
  6. // Copyright © 2018年 Haishenghai intelligence network technology. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. #import "PlaceModel.h"
  10. #import <SRWebSocket.h>
  11. #import <JPUSHService.h>
  12. #import <AVFoundation/AVFoundation.h>//音视频框架
  13. #import "CheckMessage_VC.h"
  14. @interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIPopoverControllerDelegate,UIPopoverPresentationControllerDelegate>
  15. {
  16. // SRWebSocket *_webSocket;
  17. UILabel *remindText;
  18. int _deviceStatus;
  19. int _fireAlarm;
  20. NSString *fireAlarmContent;
  21. }
  22. //- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
  23. @end
  24. static NSInteger seq = 0;
  25. @implementation ViewController
  26. ////webSocket数据包
  27. //- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message{
  28. // NSLog(@"%@",message);
  29. // if ([message isEqualToString:@"login"]) {
  30. // NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  31. // [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
  32. // NSDate *datenow = [NSDate date];
  33. // NSString *currentTimeString = [formatter stringFromDate:datenow];
  34. // UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"你的海盛海智联账号于%@在异地设备上通过密码登录。若果不是你操作,你的账号密码已经泄露。请尽快修改密码",currentTimeString] preferredStyle:UIAlertControllerStyleAlert];
  35. // [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  36. //
  37. // [_webSocket close];
  38. // //退出
  39. // UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[UserLogin_VC alloc] init]];
  40. // [UIApplication sharedApplication].keyWindow.rootViewController = nav;
  41. //
  42. // }]];
  43. // [self presentViewController:alert animated:YES completion:nil];
  44. //
  45. //
  46. // }else{
  47. //// if ([message isKindOfClass:[NSString class]]) {
  48. //// 将json字符串转换成字典
  49. // NSError *error;
  50. // NSData * getJsonData = [message dataUsingEncoding:NSUTF8StringEncoding];
  51. // NSDictionary * getDict = [NSJSONSerialization JSONObjectWithData:getJsonData options:NSJSONReadingMutableContainers error:&error];
  52. // NSDictionary *service = [getDict objectForKey:@"service"];
  53. // NSDictionary *dataDic = [service objectForKey:@"data"];
  54. // NSString *deveStatus = [dataDic objectForKey:@"deviceStatus"];
  55. // NSString *fire = [dataDic objectForKey:@"fireAlarm"];
  56. // _deviceStatus = [deveStatus intValue];
  57. // _fireAlarm = [fire intValue];
  58. // NSLog(@"这是webSocket发的数据%d--%d",_deviceStatus,_fireAlarm);
  59. //
  60. //// }
  61. // //改变状态
  62. // if (_fireAlarm==1 &&_deviceStatus==8) {
  63. // _topImageView.image = [UIImage imageNamed:@"hsh_home_alert_top"];
  64. //
  65. // }else if (_fireAlarm !=1&& _deviceStatus !=8){
  66. // _topImageView.image = [UIImage imageNamed:@"hsh_home_normal_top"];
  67. //
  68. // }else if (_fireAlarm ==1&&_deviceStatus !=8){
  69. // _topImageView.image = [UIImage imageNamed:@"hsh_home_alert_top"];
  70. //
  71. // }else if (_fireAlarm !=1&&_deviceStatus ==8){
  72. // _topImageView.image = [UIImage imageNamed:@"hsh_home_fault_top"];
  73. //
  74. // }
  75. // [self.tableView reloadData];
  76. // //重新请求数据刷新
  77. // }
  78. //
  79. //}
  80. //
  81. #pragma mark - Life Circle
  82. - (void)viewDidLoad {
  83. [super viewDidLoad];
  84. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(fireChage:) name:@"fireChange" object:nil];
  85. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(fireChage:) name:@"appQuitChange" object:nil];
  86. // Do any additional setup after loading the view, typically from a nib.
  87. //先跑viewcontrller
  88. //生成当前时间时间戳
  89. // NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
  90. // [formatter setDateStyle:NSDateFormatterMediumStyle];
  91. // [formatter setTimeStyle:NSDateFormatterShortStyle];
  92. // [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
  93. // //设置时区,这个对于时间的处理有时很重要
  94. // NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
  95. // [formatter setTimeZone:timeZone];
  96. // NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
  97. // NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];
  98. // NSString *userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
  99. // NSString *urlString = [NSString stringWithFormat:@"ws://www.hsh-iot.cn/YunGanKeJi/websocket/%@:%@",userId,timeSp];
  100. // _webSocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:urlString]];
  101. // _webSocket.delegate = self;
  102. //
  103. // [_webSocket open];
  104. [self setupUI];
  105. //集成下拉刷新控件
  106. [self setupDownRefresh];
  107. //集成上拉刷新控件
  108. // [self setupUpRefresh];
  109. //-------------------------------------------------------------------
  110. NSString *LoginID = [[NSUserDefaults standardUserDefaults]objectForKey:@"loginId"];
  111. [JPUSHService setAlias:LoginID completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
  112. [self inputResponseCode:iResCode content:iAlias andSeq:seq];
  113. } seq:[self seq]];
  114. // 注册极光通知观察者
  115. //注册通知观察者
  116. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  117. [defaultCenter addObserver:self
  118. selector:@selector(networkDidSetup:)
  119. name:kJPFNetworkDidSetupNotification
  120. object:nil];
  121. [defaultCenter addObserver:self
  122. selector:@selector(networkDidClose:)
  123. name:kJPFNetworkDidCloseNotification
  124. object:nil];
  125. [defaultCenter addObserver:self
  126. selector:@selector(networkDidRegister:)
  127. name:kJPFNetworkDidRegisterNotification
  128. object:nil];
  129. [defaultCenter addObserver:self
  130. selector:@selector(networkDidLogin:)
  131. name:kJPFNetworkDidLoginNotification
  132. object:nil];
  133. [defaultCenter addObserver:self
  134. selector:@selector(networkDidReceiveMessage:)
  135. name:kJPFNetworkDidReceiveMessageNotification
  136. object:nil];
  137. //添加alias
  138. //查询当前设置的alias
  139. // [JPUSHService getAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
  140. // [self inputResponseCode:iResCode content:iAlias andSeq:seq];
  141. // } seq:[self seq]];
  142. }
  143. - (void)inputResponseCode:(NSInteger)code content:(NSString *)content andSeq:(NSInteger)seq{
  144. NSLog (@"\n\n code:%ld content:%@ seq:%ld", code, content, seq);
  145. }
  146. - (NSInteger)seq {
  147. return ++ seq;
  148. }
  149. //与极光服务端建立长连接
  150. - (void)networkDidSetup:(NSNotification *)notification {
  151. }
  152. //长连接关闭
  153. - (void)networkDidClose:(NSNotification *)notification {
  154. }
  155. //注册成功
  156. - (void)networkDidRegister:(NSNotification *)notification {
  157. }
  158. //登录成功
  159. - (void)networkDidLogin:(NSNotification *)notification {
  160. }
  161. //自定义消息通知消息
  162. -(void)networkDidReceiveMessage:(NSNotification *)notification{
  163. NSDictionary *userInfo = [notification userInfo];
  164. NSLog(@"这是收到自定义消息通知%@",userInfo);
  165. NSError *error;
  166. NSString *content = [userInfo objectForKey:@"content"];
  167. //这是收到的推送的消息
  168. NSData *getJsonData = [content dataUsingEncoding:NSUTF8StringEncoding];
  169. NSDictionary * getDict = [NSJSONSerialization JSONObjectWithData:getJsonData options:NSJSONReadingMutableContainers error:&error];
  170. // NSLog(@"----------这是content%@",getDict);
  171. // NSUInteger messageID = [[userInfo objectForKey:@"_j_msgid"] unsignedIntegerValue];
  172. NSString *data = [getDict objectForKey:@"data"];
  173. NSData *dataString = [data dataUsingEncoding:NSUTF8StringEncoding];
  174. NSDictionary * dataDict = [NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:&error];
  175. // NSLog(@"----------这是data%@",dataDict);
  176. NSString *fireAlarm = [dataDict objectForKey:@"fireAlarm"];
  177. NSString *deviceStatus = [dataDict objectForKey:@"deviceStatus"];
  178. NSString *eventTime = [dataDict objectForKey:@"eventTime"];
  179. NSString *deviceId = [dataDict objectForKey:@"deviceId"];
  180. NSString *deviceName = [dataDict objectForKey:@"deviceName"];
  181. NSString *siteName = [dataDict objectForKey:@"siteName"];
  182. NSString *siteLocation = [dataDict objectForKey:@"siteLocation"];
  183. NSString *regionName = [dataDict objectForKey:@"regionName"];
  184. if ([fireAlarm isEqualToString:@"1"]&&[deviceStatus isEqualToString:@"0"]) {
  185. [self.tableView.mj_header beginRefreshing];
  186. //播放报警声音
  187. [self playSound];
  188. NSString *alertContent = [NSString stringWithFormat:@"发生时间:%@\n设备编号:%@\n设备名称:%@\n场所名称:%@\n安装位置:%@\n详细地址:%@\n" ,eventTime,deviceId,deviceName,siteName,regionName,siteLocation];
  189. NSLog(@"这是极光推送的消息%@",alertContent);
  190. fireAlarmContent = alertContent;
  191. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"尊敬的用户,您的设备发生火警请及时处理" message:alertContent preferredStyle:UIAlertControllerStyleAlert];
  192. [alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  193. if(self.player.playing) {
  194. [self.player stop];
  195. self.player =nil;
  196. [[NSNotificationCenter defaultCenter]removeObserver:self];
  197. [alert removeFromParentViewController];
  198. }
  199. //
  200. CheckMessage_VC *message = [[CheckMessage_VC alloc]init];
  201. message.hidesBottomBarWhenPushed = YES;
  202. [self.navigationController pushViewController:message animated:YES];
  203. }]];
  204. [self presentViewController:alert animated:YES completion:nil];
  205. }else{
  206. //故障
  207. }
  208. }
  209. #pragma mark-----收到火警消息之后的操作
  210. -(void)playSound{
  211. // 播放系统声音
  212. //定义一个SystemSoundID
  213. // SystemSoundID soundID = 1331;
  214. //// 播放声音
  215. // AudioServicesPlaySystemSound(soundID);
  216. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"fireAlarm" ofType:@"mp3"];
  217. NSURL *fileUrl = [NSURL URLWithString:filePath];
  218. self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:fileUrl error:nil];
  219. //设置声音的大小
  220. self.player.volume =1;
  221. //设置循环次数 ,如果为负数,就是无限循环
  222. self.player.numberOfLoops = -1;
  223. //准备播放
  224. [self.player prepareToPlay];
  225. //播放
  226. [self.player play];
  227. }
  228. -(void)fireChage:(NSNotification *)noti{
  229. NSLog(@"6666666666");
  230. // NSDictionary *userInfo = [noti userInfo];
  231. // NSLog(@"这是收到自定义消息通知%@",userInfo);
  232. // NSError *error;
  233. // NSString *content = [userInfo objectForKey:@"content"];
  234. //
  235. // //这是收到的推送的消息
  236. // NSData *getJsonData = [content dataUsingEncoding:NSUTF8StringEncoding];
  237. // NSDictionary * getDict = [NSJSONSerialization JSONObjectWithData:getJsonData options:NSJSONReadingMutableContainers error:&error];
  238. // // NSLog(@"----------这是content%@",getDict);
  239. // // NSUInteger messageID = [[userInfo objectForKey:@"_j_msgid"] unsignedIntegerValue];
  240. // NSString *data = [getDict objectForKey:@"data"];
  241. // NSData *dataString = [data dataUsingEncoding:NSUTF8StringEncoding];
  242. // NSDictionary * dataDict = [NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:&error];
  243. // // NSLog(@"----------这是data%@",dataDict);
  244. // NSString *fireAlarm = [dataDict objectForKey:@"fireAlarm"];
  245. // NSString *deviceStatus = [dataDict objectForKey:@"deviceStatus"];
  246. //
  247. // NSString *eventTime = [dataDict objectForKey:@"eventTime"];
  248. // NSString *deviceId = [dataDict objectForKey:@"deviceId"];
  249. // NSString *deviceName = [dataDict objectForKey:@"deviceName"];
  250. // NSString *siteName = [dataDict objectForKey:@"siteName"];
  251. // NSString *siteLocation = [dataDict objectForKey:@"siteLocation"];
  252. // NSString *regionName = [dataDict objectForKey:@"regionName"];
  253. // if ([fireAlarm isEqualToString:@"1"]&&[deviceStatus isEqualToString:@"0"]) {
  254. // //播放报警声音
  255. [self playSound];
  256. // NSString *alertContent = [NSString stringWithFormat:@"发生时间:%@\n设备编号:%@\n设备名称:%@\n场所名称:%@\n安装位置:%@\n详细地址:%@\n" ,eventTime,deviceId,deviceName,siteName,regionName,siteLocation];
  257. // NSLog(@"这是极光推送的消息%@",alertContent);
  258. //
  259. // fireAlarmContent = alertContent;
  260. [self.tableView.mj_header beginRefreshing];
  261. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"尊敬的用户,您的设备发生火警请及时处理" message:@"" preferredStyle:UIAlertControllerStyleAlert];
  262. [alert addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  263. if(self.player.playing) {
  264. [self.player stop];
  265. self.player =nil;
  266. [[NSNotificationCenter defaultCenter]removeObserver:self];
  267. [alert removeFromParentViewController];
  268. }
  269. //
  270. CheckMessage_VC *message = [[CheckMessage_VC alloc]init];
  271. message.hidesBottomBarWhenPushed = YES;
  272. [self.navigationController pushViewController:message animated:YES];
  273. }]];
  274. [self presentViewController:alert animated:YES completion:nil];
  275. }
  276. -(void)viewWillAppear:(BOOL)animated{
  277. [super viewWillAppear:animated];
  278. [self.navigationController setNavigationBarHidden:NO animated:NO];
  279. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  280. [self.navigationController.navigationBar setShadowImage:[UIImage new]];
  281. // [DataSourceManager getSiteWithUrlPagesize:@"10" Page:@"1" completionBlock:^(NSMutableArray *array) {
  282. //
  283. // _dataArray = array;
  284. // [_tableView reloadData];
  285. // }];
  286. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableDidSelected:) name:@"click" object:nil];
  287. }
  288. // 如果不想让其他页面的导航栏变为透明 需要重置
  289. //在页面消失的时候就让navigationbar还原样式
  290. -(void)viewWillDisappear:(BOOL)animated{
  291. [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
  292. [self.navigationController.navigationBar setShadowImage:nil];
  293. }
  294. -(void)viewDidDisappear:(BOOL)animated{
  295. [[NSNotificationCenter defaultCenter]removeObserver:self name:@"click" object:nil];
  296. self.navigationController.navigationBarHidden =NO;
  297. }
  298. //#pragma mark - SRWebSocketDelegate
  299. /////--------------------------------------
  300. //
  301. //- (void)webSocketDidOpen:(SRWebSocket *)webSocket;
  302. //{
  303. // NSLog(@"Websocket Connected");
  304. //}
  305. //
  306. //- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
  307. //{
  308. // NSLog(@":( Websocket Failed With Error %@", error);
  309. //
  310. //// _webSocket = nil;
  311. //}
  312. //
  313. //- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessageWithString:(nonnull NSString *)string
  314. //{
  315. // NSLog(@"Received \"%@\"", string);
  316. // // [self _addMessage:[[TCMessage alloc] initWithMessage:string incoming:YES]];
  317. //}
  318. //
  319. //- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
  320. //{
  321. // NSLog(@"WebSocket closed");
  322. // _webSocket = nil;
  323. //}
  324. //
  325. //- (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload;
  326. //{
  327. // NSLog(@"WebSocket received pong");
  328. //}
  329. - (void)didReceiveMemoryWarning {
  330. [super didReceiveMemoryWarning];
  331. // Dispose of any resources that can be recreated.
  332. }
  333. -(void)setupUI{
  334. self.view.backgroundColor = [UIColor whiteColor];
  335. /** 设置导航栏 */
  336. self.navigationItem.title =@"海盛海智联";
  337. [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
  338. UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
  339. leftButton.frame = CGRectMake(0, 0, 22, 22);
  340. [leftButton setBackgroundImage:[UIImage imageNamed:@"hsh_home_guide"] forState:UIControlStateNormal];
  341. [leftButton addTarget:self action:@selector(studyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  342. // UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
  343. UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
  344. rightButton.frame = CGRectMake(0, 0, 44, 44);
  345. [rightButton setImage:[UIImage imageNamed:@"hsh_home_add_white"] forState:UIControlStateNormal];
  346. [rightButton addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  347. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
  348. // self.navigationItem.leftBarButtonItem =leftItem;
  349. self.navigationItem.rightBarButtonItem = rightItem;
  350. _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
  351. _tableView.delegate = self;
  352. _tableView.dataSource = self;
  353. _tableView.userInteractionEnabled = YES;
  354. _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  355. [self.view addSubview:_tableView];
  356. //设置表头
  357. _topImageView = [[UIImageView alloc]init];
  358. _topImageView.frame = CGRectMake(0, 0, self.view.frame.size.width, 278);
  359. // _topImageView.contentMode = UIViewContentModeScaleToFill;
  360. _tableView.tableHeaderView = _topImageView;
  361. _topImageView.image = [UIImage imageNamed:@"hsh_home_normal_top"];
  362. self.tableView.showsVerticalScrollIndicator = NO;
  363. remindText = [[UILabel alloc]init];
  364. remindText.frame = CGRectMake(self.view.frame.size.width/2-60, self.view.frame.size.height*0.6, 120, 44);
  365. [self.tableView addSubview:remindText];
  366. remindText.textAlignment = NSTextAlignmentCenter;
  367. remindText.text = @"场所很安全";
  368. //加载GIF图 这里的contentmode 需要设置成UIViewContentModeScaleAspectFit;图片则不是
  369. /* FLAnimatedImageView *bannerImageView = [FLAnimatedImageView new];
  370. bannerImageView.contentMode = UIViewContentModeScaleAspectFill;
  371. bannerImageView.frame = CGRectMake(0, 0, self.view.frame.size.width, 250);
  372. NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]]pathForResource:@"hsh_home_normal_gif" ofType:@"gif"];
  373. NSData *imageData = [NSData dataWithContentsOfFile:filePath];
  374. bannerImageView.backgroundColor = [UIColor clearColor];
  375. bannerImageView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];
  376. */
  377. // [_tableView.tableHeaderView addSubview: _topImageView];
  378. if (@available (iOS 11.0,*)) {
  379. self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  380. }else{
  381. self.automaticallyAdjustsScrollViewInsets = NO;
  382. }
  383. }
  384. #pragma mark---上拉下拉刷新---
  385. /**
  386. * 集成上拉刷新控件
  387. */
  388. - (void)setupUpRefresh
  389. {
  390. self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreDatas)];
  391. [self.tableView.mj_footer beginRefreshing];
  392. }
  393. /**
  394. * 集成下拉刷新控件
  395. */
  396. - (void)setupDownRefresh
  397. {
  398. // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
  399. self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewDatas)];
  400. //马上进入刷新状态
  401. [self.tableView.mj_header beginRefreshing];
  402. }
  403. -(void)loadNewDatas{
  404. _pageIndex =1;//默认加载第一页
  405. [self.dataArray removeAllObjects];
  406. //请求数据
  407. //请求数据
  408. [DataSourceManager getTroubleSiteWithUrlPagesize:100 Page:_pageIndex completionBlock:^(NSMutableArray *array) {
  409. _dataArray = array;
  410. if (_dataArray.count ==0) {
  411. remindText.hidden = NO;
  412. }else{
  413. remindText.hidden = YES;
  414. }
  415. [_tableView reloadData];
  416. //拿到当前的下拉刷新控件,结束刷新
  417. [self.tableView.mj_header endRefreshing];
  418. }];
  419. }
  420. -(void)loadMoreDatas{
  421. //1.设置页数
  422. _pageIndex++;//默认加载第一页
  423. //请求数据
  424. [DataSourceManager getSiteWithUrlPagesize:100 Page:1 completionBlock:^(NSMutableArray *array) {
  425. [_dataArray addObjectsFromArray:array];
  426. [_tableView reloadData];
  427. //拿到当前的下拉刷新控件,结束刷新
  428. self.tableView.estimatedRowHeight = 0;
  429. self.tableView.estimatedSectionHeaderHeight = 0;
  430. self.tableView.estimatedSectionFooterHeight = 0;
  431. [self.tableView.mj_footer endRefreshing];
  432. }];
  433. [self.tableView.mj_footer endRefreshing];
  434. }
  435. #pragma mark-- tableviewDelegate
  436. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  437. return self.dataArray.count;
  438. }
  439. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  440. return 1;
  441. }
  442. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  443. return 60;
  444. }
  445. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  446. static NSString *cellId = @"cell";
  447. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  448. if (!cell) {
  449. cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
  450. }
  451. //让单元格的分割线和Cell一样宽
  452. cell.preservesSuperviewLayoutMargins = false;
  453. cell.separatorInset = UIEdgeInsetsZero;
  454. cell.layoutMargins = UIEdgeInsetsZero;
  455. troubleSiteModel *place = [_dataArray objectAtIndex:indexPath.row];
  456. if ([place.troubleType isEqualToString:@"1"]) {
  457. cell.imageView.image = [UIImage imageNamed:@"hsh_home_alert"];
  458. _topImageView.image = [UIImage imageNamed:@"hsh_home_alert_top"];
  459. }else if ([place.troubleType isEqualToString:@"0"]){
  460. cell.imageView.image = [UIImage imageNamed:@"hsh_home_fault"];
  461. _topImageView.image = [UIImage imageNamed:@"hsh_home_fault_top"];
  462. }else{
  463. _topImageView.image = [UIImage imageNamed:@"hsh_home_normal_top"];
  464. }
  465. cell.textLabel.text = place.deployment;
  466. cell.detailTextLabel.text =place.location;
  467. return cell;
  468. }
  469. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  470. return 60;
  471. }
  472. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  473. return 0;
  474. }
  475. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  476. UIView *headerView = [[UIView alloc]init];
  477. headerView.backgroundColor = [UIColor whiteColor];
  478. UILabel *titlelab = [[UILabel alloc]init];
  479. titlelab.frame = CGRectMake(0, 0, self.view.frame.size.width, 60);
  480. titlelab.text = @"区域场所";
  481. titlelab.font = [UIFont systemFontOfSize:24];
  482. titlelab.textAlignment = NSTextAlignmentCenter;
  483. [headerView addSubview:titlelab];
  484. UIView *line = [[UIView alloc]init];
  485. line.frame = CGRectMake(0, 59, self.view.frame.size.width, 1);
  486. line.backgroundColor = [UIColor colorWithRed:227/255.0 green:227/255.0 blue:227/255.0 alpha:1];
  487. [headerView addSubview:line];
  488. return headerView;
  489. }
  490. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  491. [tableView deselectRowAtIndexPath:indexPath animated:YES];//反选
  492. troubleSiteModel *place = [_dataArray objectAtIndex:indexPath.row];
  493. DetailDeveceList_VC *detailVC = [[DetailDeveceList_VC alloc ]init];
  494. detailVC.siteID = place.siteId;
  495. detailVC.hidesBottomBarWhenPushed =YES;
  496. [self.navigationController pushViewController:detailVC animated:YES];
  497. }
  498. #pragma mark---respose Event
  499. -(void)addBtnClick:(UIButton *)addBtn{
  500. self.itemPopVC = [[popViewController alloc] init];
  501. self.itemPopVC.modalPresentationStyle = UIModalPresentationPopover;
  502. self.itemPopVC.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem; //rect参数是以view的左上角为坐标原点(0,0)
  503. self.itemPopVC.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUnknown; //箭头方向,如果是baritem不设置方向,会默认up,up的效果也是最理想的
  504. self.itemPopVC.popoverPresentationController.delegate = self;
  505. [self presentViewController:self.itemPopVC animated:YES completion:nil];
  506. }
  507. //处理popover上的talbe的cell点击
  508. - (void)tableDidSelected:(NSNotification *)notification {
  509. NSIndexPath *indexpath = (NSIndexPath *)notification.object;
  510. switch (indexpath.row) {
  511. case 0:
  512. {
  513. AddDevece_VC *deveceVC = [[AddDevece_VC alloc]init];
  514. deveceVC.hidesBottomBarWhenPushed = YES;
  515. [self.navigationController pushViewController:deveceVC animated:YES];
  516. }
  517. break;
  518. case 1:
  519. {
  520. Addplace_VC *placeVC = [[Addplace_VC alloc]init];
  521. placeVC.hidesBottomBarWhenPushed = YES;
  522. [self.navigationController pushViewController:placeVC animated:YES];
  523. }
  524. break;
  525. }
  526. if (self.buttonPopVC) { //我暂时使用这个方法让popover消失,但我觉得应该有更好的方法,因为这个方法并不会调用popover消失的时候会执行的回调。
  527. [self.buttonPopVC dismissViewControllerAnimated:YES completion:nil];
  528. self.buttonPopVC = nil;
  529. }else{
  530. [self.itemPopVC dismissViewControllerAnimated:YES completion:nil];
  531. self.itemPopVC = nil;
  532. }
  533. }
  534. - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
  535. return UIModalPresentationNone;
  536. }
  537. - (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
  538. return YES;
  539. //no点击蒙版popover不消失, 默认yes
  540. }
  541. -(void)studyBtnClick:(UIButton *)studyBtn{
  542. }
  543. - (void)dealloc {
  544. // [self unObserveAllNotifications];
  545. }
  546. - (void)unObserveAllNotifications {
  547. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  548. [defaultCenter removeObserver:self
  549. name:kJPFNetworkDidSetupNotification
  550. object:nil];
  551. [defaultCenter removeObserver:self
  552. name:kJPFNetworkDidCloseNotification
  553. object:nil];
  554. [defaultCenter removeObserver:self
  555. name:kJPFNetworkDidRegisterNotification
  556. object:nil];
  557. [defaultCenter removeObserver:self
  558. name:kJPFNetworkDidLoginNotification
  559. object:nil];
  560. [defaultCenter removeObserver:self
  561. name:kJPFNetworkDidReceiveMessageNotification
  562. object:nil];
  563. [defaultCenter removeObserver:self
  564. name:kJPFServiceErrorNotification
  565. object:nil];
  566. }
  567. @end