PolicyViewController.m 10 KB

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