// // MessageViewController.m // watch // // Created by xTT on 2017/7/13. // Copyright © 2017年 xTT. All rights reserved. // #import "RemindMessageViewController.h" #import "DMessage.h" #import "MessageTableViewCell.h" @interface RemindMessageViewController () @property (nonatomic ,strong) NSMutableArray *listDataSoure; @property (nonatomic ,strong) NSMutableArray *dateArr; @end @implementation RemindMessageViewController - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self loadMoreDataWithDMessage:nil]; } - (void)viewDidLoad { [super viewDidLoad]; self.myTableView.frame = CGRectMake(-4, 0, SWIDTH+8, self.myTableView.bounds.size.height-40); self.view.backgroundColor = KKBackgroundGrey; [UICommon setTavleViewlink:self.myTableView distance:16]; [self.view addSubview:self.myTableView]; self.zx_hideBaseNavBar = YES; // Do any additional setup after loading the view. self.listDataSoure = [[NSMutableArray alloc] init]; self.dateArr = [[NSMutableArray alloc] init]; WEAKSELF self.myTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{ [weakSelf loadMoreDataWithDMessage:[weakSelf.listDataSoure lastObject]]; }]; [self.myTableView registerNib:[UINib nibWithNibName:@"MessageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MessageTableViewCellID"]; // [self.myTableView.mj_footer beginRefreshing]; [self loadMoreDataWithDMessage:[weakSelf.listDataSoure lastObject]]; self.myTableView.tableFooterView = [[UIView alloc] init]; // iOS 11 系统 tableView 会自动调整高度 在 Gounp 模式下 头部的高度会有问题 ,需要对 预设的高度设置为0 self.myTableView.estimatedRowHeight = 0; self.myTableView.estimatedSectionHeaderHeight = 0; self.myTableView.estimatedSectionFooterHeight = 0; } //MARK :刪除方法 -(void)deletAction{ // NSMutableArray * messageIds = [NSMutableArray array]; // for (NSArray* secArr in self.myDataSource){ // for (DMessage *dMsg in secArr) { // if(dMsg.isSelect){ // [messageIds addObject: dMsg.id]; // } // // } // } WEAKSELF CommonPopView *popView = [CommonPopView new]; [popView subPopView:popViewType_binding PopTitle:@"注意" PopText:@"确认要清空提醒消息?" okTouchBlock:^{ [DMessage delectTrackWithMessageIds:nil success:^(id responseObject) { [weakSelf.myDataSource removeAllObjects]; [weakSelf.listDataSoure removeAllObjects]; [weakSelf.dateArr removeAllObjects]; [weakSelf loadMoreDataWithDMessage:nil]; xLog(@"删除记录成功!") } failure:^{ [SVProgressHUD showSuccessWithStatus:@"删除记录失败,请稍后重试..."]; } ]; }]; } - (void)loadMoreDataWithDMessage:(DMessage *)dMessage{ WEAKSELF NSString *ID; if(dMessage){ ID = dMessage.id; } [DMessage getObjsWithID:ID success:^(NSMutableArray *arr) { if(!ID){ [weakSelf.listDataSoure removeAllObjects]; [weakSelf.dateArr removeAllObjects]; [weakSelf.myDataSource removeAllObjects]; } [weakSelf.listDataSoure addObjectsFromArray:arr]; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [weakSelf.listDataSoure enumerateObjectsUsingBlock:^(DMessage *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSDate *date = [NSDate dateWithTimeIntervalSince1970:[obj.timestamp doubleValue]]; NSString *dateStr = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" date:date]; NSMutableArray *arr = dic[dateStr]; if (!arr) { arr = [NSMutableArray array]; } [arr addObject:obj]; [dic setObject:arr forKey:dateStr]; if (![weakSelf.dateArr containsObject:dateStr]) { [weakSelf.dateArr addObject:dateStr]; } }]; [weakSelf.myDataSource removeAllObjects]; [weakSelf.dateArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [weakSelf.myDataSource addObject:dic[obj]]; }]; [weakSelf.myTableView reloadData]; if ([[weakSelf.myDataSource firstObject] count] == 0){ weakSelf.navigationItem.rightBarButtonItem = nil;; } [weakSelf.myTableView.mj_footer endRefreshing]; } failure:^(NSError *error){ if (!error) { // [self setRightBarBtnTitle:nil RoImage:[UIImage imageNamed:@"icon_delete"]]; } [weakSelf.myTableView.mj_footer endRefreshing]; }]; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleNone; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 40; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ [[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITableViewHeaderFooterView class]]] setTextAlignment:NSTextAlignmentCenter]; //[[UILabel appearanceWhenContainedInInstancesOfClasses:@[[UITableViewHeaderFooterView class]]] setFont:[UIFont systemFontOfSize:12.0 weight:UIFontWeightThin]]; return self.dateArr[section]; } - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section { if([view isKindOfClass:[UITableViewHeaderFooterView class]]) { UITableViewHeaderFooterView *tableViewHeaderFooterView = (UITableViewHeaderFooterView *) view; tableViewHeaderFooterView.textLabel.textAlignment = NSTextAlignmentCenter; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ DMessage *dMessage = self.myDataSource[indexPath.section][indexPath.row]; NSString *str = @""; if ([dMessage.type isEqualToString:@"sos"]) { str = [NSString stringWithFormat:@"【%@】触发SOS!",dMessage.name]; str = [NSString stringWithFormat:@"%@\n当前位置:%@",str,dMessage.address]; }else if ([dMessage.type isEqualToString:@"sms"]) { str = [NSString stringWithFormat:@"【%@】SIM卡收到短信\n发件人:%@\n内容:%@",dMessage.name,dMessage.phone,dMessage.content]; }else if ([dMessage.type isEqualToString:@"rail"]) { str = [NSString stringWithFormat:@"【%@】进入安全区域 %@",dMessage.name,dMessage.railName]; }else if ([dMessage.type isEqualToString:@"lowBattery"]) { str = [NSString stringWithFormat:@"【%@】设备当前电量%@%%",dMessage.name,dMessage.battery]; }else if ([dMessage.type isEqualToString:@"photograph"]) { str = [NSString stringWithFormat:@"【%@】上传图册",dMessage.name]; } if (dMessage.msgContent.length > 0) { str = dMessage.msgContent; } return [self getLineHeight:str withWidth:SWIDTH*(280.0/320) withFont:[UIFont systemFontOfSize:13]]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ DMessage *dMessage = self.myDataSource[indexPath.section][indexPath.row]; MessageTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MessageTableViewCellID" forIndexPath:indexPath]; NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dMessage.timestamp doubleValue]]; //[UICommon tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath cellCoutn:[self.myDataSource[indexPath.section] count]]; cell.timeLabel.text = [myHelper getDateFormatWithStr:@"HH:mm" date:date]; //时间 if (dMessage.isSelect){ cell.selectImageView.image = [UIImage imageNamed:@"选中"]; }else{ cell.selectImageView.image = [UIImage imageNamed:@"CellGraySelected"]; } //信息 NSString * titleStr ; if ([dMessage.type isEqualToString:@"sos"]) { titleStr = @"求救报警"; NSString *tmp = [NSString stringWithFormat:@"【%@】触发SOS!",dMessage.name]; NSInteger lenght = tmp.length; tmp = [NSString stringWithFormat:@"%@\n当前位置:%@",tmp,dMessage.address]; NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:tmp]; [att addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,lenght)]; cell.messageLabel.attributedText = att; }else if ([dMessage.type isEqualToString:@"sms"]) { titleStr = @"短信通知"; cell.messageLabel.text = [NSString stringWithFormat:@"【%@】SIM卡收到短信\n发件人:%@\n内容:%@",dMessage.name,dMessage.phone,dMessage.content]; }else if ([dMessage.type isEqualToString:@"rail"]) { titleStr = @"区域报警"; if ([dMessage.action isEqualToString:@"enter"]) { cell.messageLabel.text = [NSString stringWithFormat:@"【%@】进入安全区域 【%@】",dMessage.name,dMessage.railName]; }else if ([dMessage.action isEqualToString:@"leave"]){ cell.messageLabel.text = [NSString stringWithFormat:@"【%@】离开安全区域 【%@】",dMessage.name,dMessage.railName]; } }else if ([dMessage.type isEqualToString:@"lowBattery"]) { titleStr = @"低电报警"; cell.messageLabel.text = [NSString stringWithFormat:@"【%@】设备当前电量%@%%",dMessage.name, dMessage.battery]; }else if ([dMessage.type isEqualToString:@"photograph"]) { titleStr = @"图册上传"; cell.messageLabel.text = [NSString stringWithFormat:@"【%@】上传图册",dMessage.name]; }else if ([dMessage.type isEqualToString:@"abnormalBodyTemperature"]) { titleStr = @"体温异常报警"; if(dMessage.msgContent.length > 0){ cell.messageLabel.text = dMessage.msgContent; }else cell.messageLabel.text = [NSString stringWithFormat:@"【%@】设备当前监测体温为%@℃",dMessage.name, dMessage.data]; }else if ([dMessage.type isEqualToString:@"abnormalHeartRate"]) { titleStr = @"心率异常报警"; if(dMessage.msgContent.length > 0){ cell.messageLabel.text = dMessage.msgContent; }else cell.messageLabel.text = [NSString stringWithFormat:@"【%@】设备当前监测心率为%@次/分",dMessage.name, dMessage.data]; } if(dMessage.msgContent.length > 0){ cell.messageLabel.text = dMessage.msgContent; } if(dMessage.read && [dMessage.read intValue] == 0) { cell.readLabel.hidden = NO; }else{ cell.readLabel.hidden = YES; } cell.aTitleLabel.text = titleStr; cell.backgroundColor = KKWhiteColorColor; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ DMessage *dMessage = self.myDataSource[indexPath.section][indexPath.row]; if (dMessage.read && [dMessage.read intValue] == 0) { WEAKSELF [cUser updateMsg2Read:dMessage.msgId MsgType:@"message" Success:^(id responseObject) { [weakSelf refreshTableView:dMessage]; NSLog(@"%@", responseObject); } failure:^{ }]; } } -(void)refreshTableView:(DMessage *)message { message.read = @1; [self.myTableView reloadData]; } - (CGFloat)getLineHeight:(NSString *)string withWidth:(CGFloat)width withFont:(UIFont *)font{ // NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:string]; // if (font) { // [att addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)]; // } // // CGSize size = [att boundingRectWithSize:CGSizeMake(width, MAXFLOAT) // options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading // context:nil].size; // // // NSMutableAttributedString *attTest = [[NSMutableAttributedString alloc] initWithString:@"test"]; // if (font) { // [attTest addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, 4)]; // } // // CGSize sizeTest = [attTest boundingRectWithSize:CGSizeMake(width, MAXFLOAT) // options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading // context:nil].size; // // CGFloat line = size.height/sizeTest.height; // // if (line == 1) { // return 66; // } // CGFloat height = (line + 1.2) * sizeTest.height; // if(height < 66){ // height = 66; // } // return height; UILabel *label = [[UILabel alloc]init]; label.numberOfLines = 0; label.lineBreakMode = NSLineBreakByWordWrapping; label.textAlignment = NSTextAlignmentLeft; label.text = string; label.font = font; CGSize labelSize = [label sizeThatFits:CGSizeMake(width, MAXFLOAT)]; CGFloat height = ceil(labelSize.height) + 1 + 34 + 10 +8; //换行符的 行数 NSUInteger number = [[string componentsSeparatedByString:@"\n"] count]; if(number > 1){ height += (number-1) * 21; } // xLog(@"文本框应该高 Height :%f ", height); if(height < 66){ height = 66; } return height; } @end