// // MyMsgVC.m // jiaPeiC // // Created by apple on 15/12/17. // Copyright © 2015年 JCZ. All rights reserved. // #import "MyMsgVC.h" #import "MsgListCell.h" #import "ViewController.h" #import "MyUINavigationController.h" @interface MyMsgVC () { NSArray* models; UITableView* myTableView; HolderView* holderV; int currentPage; } @end @implementation MyMsgVC - (void)viewDidLoad { [super viewDidLoad]; [self myInit]; [self getMessageInfos]; } -(void)viewWillDisappear:(BOOL)animated { //所有有请求的页面都要加 [super viewWillDisappear:animated]; RemoveHUD(); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark - -(void)myInit { self.title = @"我的消息"; if (_isNotification == NO) { //正常状态 [self configNavBar]; } else { //推送状态 要将根视图变回去 UIBarButtonItem* backBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissNavgation)]; [backBtn setTintColor:defGreen]; // self.navigationController.navigationBar.translucent = NO; [self.navigationItem setLeftBarButtonItem:backBtn]; } currentPage = 1; myTableView = [[UITableView alloc] initWithFrame:kFrame]; [myTableView setDelegate:self]; [myTableView setDataSource:self]; myTableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.view addSubview:myTableView]; holderV = [[HolderView alloc] initWithFrame:kFrame]; holderV.y = kNavOffSet; [holderV freshBlock:^{ [self getMessageInfos]; }]; [self addV:holderV]; } -(void)dismissNavgation { //推送过来 返回主页面 不晓得能不能行--推送 待测试 dansonmark ViewController *vc = [ViewController new]; MyUINavigationController *nav = [[MyUINavigationController alloc] initWithRootViewController:vc]; [UIApplication sharedApplication].delegate.window.rootViewController = nav; } #pragma mark - - (void)getMessageInfos { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"user" Value:defUser.sfzmhm]; [arr addPro:@"msgType" Value:@""]; [arr addPro:@"userType" Value:@"2"]; [arr addPro:@"isPage" Value:@"1"]; [arr addPro:@"pageSize" Value:@"10"]; [arr addPro:@"currentPage" Value:[NSString stringWithFormat:@"%d",currentPage]]; NSString* method = @"getMessageInfos"; ShowHUD(); [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { RemoveHUD(); //NSLog(@"root---->%@",root); if (!root) { ShowMsgFailed(); [holderV setHidden:NO]; return ; } if ([root[@"code"] isEqualToString:@"1"]) { ShowMsgFailed(); [holderV setHidden:NO]; return; } models = root[@"body"]; if (models.count > 0) { currentPage ++; [myTableView reloadData]; [holderV setHidden:YES]; }else{ [holderV setHidden:NO]; } }]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return models.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MsgListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[MsgListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } NSDictionary *model = models[indexPath.row]; [cell.dateBtn setTitle:model[@"CRDATE"] textColor:[UIColor whiteColor] font:15 fotState:UIControlStateNormal]; NSString *str = model[@"PHOTO"]; if (![str hasPrefix:@"http"]){ str = [imgPreFix stringByAppendingString:str]; } [cell.headImgView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"noHeadImg.png"]]; NSString *contentString = model[@"CONTENT"]; cell.messageContentLabel.text = contentString; //重新设置文本和气泡的大小 float newHeight = [self getRectWithPostContent:contentString]; CGRect newRect = cell.backImageView.frame; newRect.size.height = newHeight + 10; cell.backImageView.frame = newRect; newRect = cell.messageContentLabel.frame; newRect.size.height = newHeight; cell.messageContentLabel.frame = newRect; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary * dic = models[indexPath.row]; NSString * content = dic[@"CONTENT"]; return [self getRectWithPostContent:content] + 50; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ return [UIView new]; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return .1; } -(float)getRectWithPostContent:(NSString *)content { CGRect rect = [content boundingRectWithSize:CGSizeMake(kSize.width - 155, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont scaleSize:17] } context:nil]; //整数位取整加1或者等于自己 float height = ceilf(rect.size.height); //得出的高是文本的高 文本所在的label距离cell的边框有一定的距离 所以加上20 return height+ 30; } @end