NewMessageViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // NewMessageViewController.m
  3. // jitao
  4. //
  5. // Created by 罗云飞 on 2018/9/5.
  6. // Copyright © 2018年 罗云飞. All rights reserved.
  7. //
  8. #import "NewMessageViewController.h"
  9. #import "NewMeassageCell.h"
  10. #import "NewMeassageModel.h"
  11. #import "NewMsgDetailViewController.h"
  12. @interface NewMessageViewController ()<UITableViewDelegate,UITableViewDataSource>{
  13. UITableView *newtableView;
  14. NSMutableArray *dataArray;
  15. int pageIndex;
  16. int pageSize;
  17. int pageNumber;
  18. UIView *showView;
  19. }
  20. @end
  21. @implementation NewMessageViewController
  22. - (void)viewWillAppear:(BOOL)animated
  23. {
  24. [super viewWillAppear:animated];
  25. [self networkRequest:nil];
  26. [newtableView reloadData];
  27. }
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. [self setNavTitle:@"消息"];
  31. [self dataInitialization];
  32. [self loadsView];
  33. [self addRefreshing];
  34. [self networkRequest:nil];
  35. // Do any additional setup after loading the view.
  36. }
  37. - (void)dataInitialization{
  38. dataArray = NewMutableArrayInit;
  39. pageIndex = 1;
  40. pageSize = 10;
  41. pageNumber = 1;
  42. }
  43. - (void)networkRequest:(id)object
  44. {
  45. int pageNumberIndex=1;
  46. if (!object) {
  47. // [MBProgressHUD showLoadToView:self.view title:@"请稍后..."];
  48. }else if ([object isKindOfClass:[MJRefreshNormalHeader class]]) {
  49. pageNumberIndex = 1;
  50. }else if ([object isKindOfClass:[MJRefreshBackNormalFooter class]]) {
  51. pageNumberIndex = pageNumber+1;
  52. }
  53. NSMutableDictionary *parameters = NewMutableDictionaryInit;
  54. [parameters safeSetObject:@(pageSize) forKey:@"pageSize"];
  55. [parameters safeSetObject:@(pageNumberIndex) forKey:@"pageNo"];
  56. [NetworkRequestManager requestGetWithInterfacePrefix:JT_NewapplistMessage parameters:parameters onSuccess:^(id requestData) {
  57. [self dismiss:object];
  58. if ([requestData[@"error"] count] !=0) {
  59. for (NSDictionary *dic in requestData[@"error"]) {
  60. [MBProgressHUD showError:dic[@"message"] toView:self.view];
  61. }
  62. }else{
  63. pageNumber = pageNumberIndex;
  64. if ([object isKindOfClass:[MJRefreshNormalHeader class]] || pageNumber==1) {
  65. [dataArray removeAllObjects];
  66. }else if ([object isKindOfClass:[MJRefreshBackNormalFooter class]]) {
  67. NSMutableArray *arr = requestData[@"data"][@"list"];
  68. if ([arr count]<=0) {
  69. [[[UIApplication sharedApplication].delegate window] makeToast:NewConnectServerNoMoreDataTitle duration:1.0 position:CSToastPositionBottom];
  70. return;
  71. }
  72. }
  73. for (NSDictionary *dic in requestData[@"data"][@"list"]) {
  74. NewMeassageModel *model = [[NewMeassageModel alloc] initWithDictionary:dic error:nil];
  75. [dataArray addObject:model];
  76. }
  77. //查询不到类型底图
  78. if (dataArray.count<=0) {
  79. [self addErrorLoadingFrame:CGRectMake(0, NavHeader, newtableView.width, newtableView.height) title:@"暂无数据" buttonTitle:nil imageString:NewNoDataErrorImage];
  80. }else {
  81. [self hideReloadingview];
  82. [newtableView reloadData];
  83. }
  84. }
  85. } onFailure:^{
  86. [self dismiss:object];
  87. }];
  88. }
  89. #pragma mark ----加载界面
  90. - (void)loadsView{
  91. [self.view addSubview:newtableView = [NewControlPackage tableViewInitWithFrame:CGRectMake(0, NavHeader, SCREEN_WIDTH, SCREEN_HEIGHT-(NavHeader)) backgroundColor:NewGroupTableViewBackgroundColor style:1 delegate:self dataSource:self showsHorizontalScrollIndicator:NO showsVerticalScrollIndicator:NO hidden:NO tag:100 userInteractionEnabled:YES]];
  92. newtableView.separatorStyle = NO;
  93. }
  94. #pragma mark-------------------------UITableView------------------------------------
  95. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  96. {
  97. return 1;
  98. }
  99. //返回每段行数
  100. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  101. {
  102. return dataArray.count;
  103. }
  104. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. static NSString *Cell = @"Cell";
  107. NewMeassageCell * cell = [tableView dequeueReusableCellWithIdentifier:Cell];
  108. if (cell == nil) {
  109. cell = [[NewMeassageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Cell];
  110. }
  111. [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//(这种是没有点击后的阴影效果)
  112. cell.accessoryType = UITableViewCellAccessoryNone;
  113. if (dataArray.count>0) {
  114. [cell assignment:dataArray[indexPath.row]];
  115. }
  116. return cell;
  117. }
  118. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  119. {
  120. return 100;
  121. }
  122. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  123. NewMsgDetailViewController *vc = [[NewMsgDetailViewController alloc] init];
  124. NewMeassageModel *model = dataArray[indexPath.row];
  125. vc.ID = model.ID;
  126. NewPushViewController(vc);
  127. }
  128. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  129. return CGFLOAT_MIN;
  130. }
  131. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  132. return CGFLOAT_MIN;
  133. }
  134. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  135. return [[UIView alloc] init];
  136. }
  137. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  138. return [[UIView alloc] init];
  139. }
  140. #pragma mark - 上拉下拉初始化
  141. - (void)addRefreshing
  142. {
  143. __weak typeof(self) weakSelf = self;
  144. __weak UITableView *tableView = newtableView;
  145. tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  146. if ([NetworkRequestManager connectedToNetwork]) {
  147. [weakSelf networkRequest:tableView.mj_header];
  148. }else{
  149. [tableView.mj_header endRefreshing];
  150. }
  151. }];
  152. tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  153. if ([NetworkRequestManager connectedToNetwork]) {
  154. [weakSelf networkRequest:tableView.mj_footer];
  155. }else{
  156. [tableView.mj_footer endRefreshing];
  157. }
  158. }];
  159. }
  160. - (void)dismiss:(id)object
  161. {
  162. if ([object isKindOfClass:[MJRefreshNormalHeader class]]) {
  163. __weak UITableView *newvc = newtableView;
  164. [newvc.mj_header endRefreshing];
  165. }else if ([object isKindOfClass:[MJRefreshBackNormalFooter class]]){
  166. __weak UITableView *newvc = newtableView;
  167. [newvc.mj_footer endRefreshing];
  168. }else {
  169. [MBProgressHUD hideHUDForView:self.view];
  170. }
  171. }
  172. #pragma mark- 外部触发自动刷新
  173. - (void)autoDownRefresh
  174. {
  175. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  176. [self autoRefresh];
  177. });
  178. }
  179. #pragma mark- 网络请求
  180. - (void)autoRefresh
  181. {
  182. __weak UITableView *tableView = newtableView;
  183. [tableView.mj_header beginRefreshing];
  184. }
  185. #pragma mark 初始化展示框
  186. - (void) addErrorLoadingFrame:(CGRect)frame title:(NSString *)text buttonTitle:(NSString *)buttonText imageString:(NSString *)image
  187. {
  188. if(!showView)
  189. {
  190. [self.view addSubview:showView = [[UIView alloc] initWithFrame:frame]];
  191. [showView setHidden:NO];
  192. [showView setBackgroundColor:NewBgGrayColor];
  193. UIImage *NOimg = [UIImage imageNamed:image];
  194. UIImageView *tempIcon = [[UIImageView alloc]initWithImage:NOimg];
  195. [showView addSubview:tempIcon];
  196. tempIcon.sd_layout
  197. .leftSpaceToView(showView, (showView.width/2)-(NOimg.size.width/4.0f))
  198. .topSpaceToView(showView, (showView.height/2)-100.0f-(NOimg.size.height/4.0f))
  199. .widthIs(NOimg.size.width/2.0f)
  200. .heightIs(NOimg.size.height/2.0f);
  201. UILabel *tipLab = [UILabel new];
  202. [showView addSubview:tipLab];
  203. [tipLab setText:text];
  204. [tipLab setTextAlignment:NSTextAlignmentCenter];
  205. [tipLab setTextColor:NewLightGrayColor];
  206. [tipLab setBackgroundColor:NewClearColor];
  207. [tipLab setFont:NewAutoFont(16.0)];
  208. tipLab.sd_layout
  209. .leftEqualToView(showView)
  210. .rightEqualToView(showView)
  211. .topSpaceToView(tempIcon,10)
  212. .heightIs(20.0);
  213. if (buttonText.length>0) {
  214. UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
  215. [showView addSubview:button];
  216. [button setTitle:buttonText forState:UIControlStateNormal];
  217. [button setTitleColor:NewNavigationColor forState:UIControlStateNormal];
  218. ViewBorderRadius(button, 16, 2, NewNavigationColor);
  219. [button addTarget:self action:@selector(reloadingData) forControlEvents:UIControlEventTouchUpInside];
  220. CGFloat width = [NewUtils heightforString:[NSString stringWithFormat:@"%@",buttonText] andHeight:34 fontSize:16];
  221. button.sd_layout
  222. .leftSpaceToView(showView, (showView.width/2)-((width+35)/2))
  223. .topSpaceToView(tipLab, 30.0)
  224. .widthIs(width+35)
  225. .heightIs(34);
  226. }
  227. }
  228. [UIView beginAnimations:@"ShowArrow" context:nil];
  229. [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
  230. [UIView setAnimationDuration:0.25f];
  231. [UIView setAnimationDelegate:self];
  232. [showView setAlpha:1.0f];
  233. [UIView commitAnimations];
  234. }
  235. - (void)hideReloadingview
  236. {
  237. [UIView animateWithDuration:0.25 animations:^{
  238. showView.hidden = YES;
  239. } completion:^(BOOL finished) {
  240. for (id obj in showView.subviews) {
  241. [obj removeFromSuperview];
  242. }
  243. showView = nil;
  244. [showView removeFromSuperview];
  245. }];
  246. }
  247. - (void)didReceiveMemoryWarning {
  248. [super didReceiveMemoryWarning];
  249. // Dispose of any resources that can be recreated.
  250. }
  251. /*
  252. #pragma mark - Navigation
  253. // In a storyboard-based application, you will often want to do a little preparation before navigation
  254. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  255. // Get the new view controller using [segue destinationViewController].
  256. // Pass the selected object to the new view controller.
  257. }
  258. */
  259. @end