MyMsgVC.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // MyMsgVC.m
  3. // jiaPeiC
  4. //
  5. // Created by apple on 15/12/17.
  6. // Copyright © 2015年 JCZ. All rights reserved.
  7. //
  8. #import "MyMsgVC.h"
  9. #import "MsgListCell.h"
  10. #import "ViewController.h"
  11. #import "MyUINavigationController.h"
  12. @interface MyMsgVC () <UITableViewDataSource,UITableViewDelegate>
  13. {
  14. NSArray* models;
  15. UITableView* myTableView;
  16. HolderView* holderV;
  17. int currentPage;
  18. }
  19. @end
  20. @implementation MyMsgVC
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [self myInit];
  24. [self getMessageInfos];
  25. }
  26. -(void)viewWillDisappear:(BOOL)animated
  27. {
  28. //所有有请求的页面都要加
  29. [super viewWillDisappear:animated];
  30. RemoveHUD();
  31. }
  32. - (void)didReceiveMemoryWarning {
  33. [super didReceiveMemoryWarning];
  34. }
  35. #pragma mark -
  36. -(void)myInit
  37. {
  38. self.title = @"我的消息";
  39. if (_isNotification == NO)
  40. {
  41. //正常状态
  42. [self configNavBar];
  43. }
  44. else
  45. {
  46. //推送状态 要将根视图变回去
  47. UIBarButtonItem* backBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissNavgation)];
  48. [backBtn setTintColor:defGreen];
  49. // self.navigationController.navigationBar.translucent = NO;
  50. [self.navigationItem setLeftBarButtonItem:backBtn];
  51. }
  52. currentPage = 1;
  53. myTableView = [[UITableView alloc] initWithFrame:kFrame];
  54. [myTableView setDelegate:self];
  55. [myTableView setDataSource:self];
  56. myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  57. [self.view addSubview:myTableView];
  58. holderV = [[HolderView alloc] initWithFrame:kFrame];
  59. holderV.y = kNavOffSet;
  60. [holderV freshBlock:^{
  61. [self getMessageInfos];
  62. }];
  63. [self addV:holderV];
  64. }
  65. -(void)dismissNavgation
  66. {
  67. //推送过来 返回主页面 不晓得能不能行--推送 待测试 dansonmark
  68. ViewController *vc = [ViewController new];
  69. MyUINavigationController *nav = [[MyUINavigationController alloc] initWithRootViewController:vc];
  70. [UIApplication sharedApplication].delegate.window.rootViewController = nav;
  71. }
  72. #pragma mark -
  73. - (void)getMessageInfos
  74. {
  75. if (![Util connectedToNetWork]) {
  76. showMsgUnconnect();
  77. return;
  78. }
  79. NSMutableArray *arr=[NSMutableArray array];
  80. [arr addPro:@"user" Value:defUser.sfzmhm];
  81. [arr addPro:@"msgType" Value:@""];
  82. [arr addPro:@"userType" Value:@"2"];
  83. [arr addPro:@"isPage" Value:@"1"];
  84. [arr addPro:@"pageSize" Value:@"10"];
  85. [arr addPro:@"currentPage" Value:[NSString stringWithFormat:@"%d",currentPage]];
  86. NSString* method = @"getMessageInfos";
  87. ShowHUD();
  88. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  89. RemoveHUD();
  90. //NSLog(@"root---->%@",root);
  91. if (!root) {
  92. ShowMsgFailed();
  93. [holderV setHidden:NO];
  94. return ;
  95. }
  96. if ([root[@"code"] isEqualToString:@"1"]) {
  97. ShowMsgFailed();
  98. [holderV setHidden:NO];
  99. return;
  100. }
  101. models = root[@"body"];
  102. if (models.count > 0) {
  103. currentPage ++;
  104. [myTableView reloadData];
  105. [holderV setHidden:YES];
  106. }else{
  107. [holderV setHidden:NO];
  108. }
  109. }];
  110. }
  111. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  112. {
  113. return models.count;
  114. }
  115. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117. MsgListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  118. if (cell == nil)
  119. {
  120. cell = [[MsgListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  121. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  122. }
  123. NSDictionary *model = models[indexPath.row];
  124. [cell.dateBtn setTitle:model[@"CRDATE"] textColor:[UIColor whiteColor] font:15 fotState:UIControlStateNormal];
  125. NSString *str = model[@"PHOTO"];
  126. if (![str hasPrefix:@"http"]){
  127. str = [imgPreFix stringByAppendingString:str];
  128. }
  129. [cell.headImgView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"noHeadImg.png"]];
  130. NSString *contentString = model[@"CONTENT"];
  131. cell.messageContentLabel.text = contentString;
  132. //重新设置文本和气泡的大小
  133. float newHeight = [self getRectWithPostContent:contentString];
  134. CGRect newRect = cell.backImageView.frame;
  135. newRect.size.height = newHeight + 10;
  136. cell.backImageView.frame = newRect;
  137. newRect = cell.messageContentLabel.frame;
  138. newRect.size.height = newHeight;
  139. cell.messageContentLabel.frame = newRect;
  140. return cell;
  141. }
  142. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  143. {
  144. NSDictionary * dic = models[indexPath.row];
  145. NSString * content = dic[@"CONTENT"];
  146. return [self getRectWithPostContent:content] + 50;
  147. }
  148. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  149. return [UIView new];
  150. }
  151. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  152. {
  153. return .1;
  154. }
  155. -(float)getRectWithPostContent:(NSString *)content
  156. {
  157. CGRect rect = [content boundingRectWithSize:CGSizeMake(kSize.width - 155, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont scaleSize:17] } context:nil];
  158. //整数位取整加1或者等于自己
  159. float height = ceilf(rect.size.height);
  160. //得出的高是文本的高 文本所在的label距离cell的边框有一定的距离 所以加上20
  161. return height+ 30;
  162. }
  163. @end