ShareVC.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. //
  2. // ShareVC.m
  3. // jiaPei
  4. //
  5. // Created by apple on 16/4/11.
  6. // Copyright © 2016年 JCZ. All rights reserved.
  7. //
  8. #import "ShareVC.h"
  9. #import "HolderView.h"
  10. #import "newDetailVC.h"
  11. #import "NewListNoImg.h"
  12. #import "NewsDetVC.h"
  13. #import "Tools.h"
  14. @interface ShareVC ()<UITableViewDelegate,UITableViewDataSource>
  15. {
  16. NSArray *superMenu;
  17. NSArray *childMenu;
  18. NSString *selectID;
  19. NSString *searchID;
  20. NSInteger lastID;
  21. NSInteger currentPage;
  22. NSMutableArray *newsList;
  23. BOOL isChild;
  24. UILabel *lineLabel;
  25. UILabel *childMenuLabel;
  26. UILabel *holderLabel;
  27. UIView *childView;
  28. UIScrollView *supMenuScroll;
  29. UITableView *mainTableView;
  30. UITableView *childTableView;
  31. HolderView *placeHolder;
  32. }
  33. @end
  34. @implementation ShareVC
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. self.navigationController.navigationBar.backgroundColor = backGroundColor;
  38. self.navigationController.navigationBar.translucent = NO;
  39. self.view.backgroundColor = [UIColor whiteColor];
  40. self.title = @"驾考新闻";
  41. [self configNavigationBar];
  42. newsList = [NSMutableArray array];
  43. searchID = @"1";
  44. selectID = @"1";
  45. currentPage = 1;
  46. lastID = 1;
  47. [self getNewsItemByPidAndTypeWithID:@"0"];
  48. }
  49. -(void)creatMainUI
  50. {
  51. //只是为了让第一个为滑动视图的时候不偏移 那这样的话 以前的demo应该都可以用 之前虐了自己那么久的东西 以后就这样解决吧
  52. UIView *firstView = [UIView new];
  53. [self.view addSubview:firstView];
  54. supMenuScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kSize.width, 46 )];
  55. supMenuScroll.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
  56. UIButton *btn = nil;
  57. CGFloat wid = 85;
  58. for (int i = 0; i < superMenu.count; i++)
  59. {
  60. NSDictionary *dic = superMenu[i];
  61. btn = [UIButton buttonWithType:UIButtonTypeCustom];
  62. btn.frame = CGRectMake(wid*i, 0, wid, 46);
  63. if (i == superMenu.count-1) {
  64. btn.width += 10;
  65. }
  66. [btn setTitle:dic[@"NAME"] textColor:kTitleColor font:Font17 fotState:UIControlStateNormal];
  67. [btn setTitle:dic[@"NAME"] textColor:defGreen font:Font17 fotState:UIControlStateSelected];
  68. btn.titleLabel.font = [UIFont systemFontOfSize:17];
  69. btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  70. if (i == 0)
  71. {
  72. btn.selected = YES;
  73. }
  74. [btn target:self tag:i + 1];
  75. [supMenuScroll addSubview:btn];
  76. }
  77. lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 44, 75, 2)];
  78. [lineLabel setBackgroundColor:defGreen];
  79. [supMenuScroll addSubview:lineLabel];
  80. [supMenuScroll setShowsHorizontalScrollIndicator:NO];
  81. supMenuScroll.contentSize = CGSizeMake(superMenu.count * wid + 10, 46);
  82. [self.view addSubview:supMenuScroll];
  83. CGFloat x,y,w,h;
  84. x = 0;
  85. y = 46;
  86. w = kSize.width;
  87. h = 40;
  88. UIView *childMenuBar = [[UIView alloc] setxywh];
  89. [childMenuBar borderColor:KlineColor width:1 cornorRadios:0];
  90. childMenuBar.backgroundColor = backGroundColor;
  91. [self.view addSubview:childMenuBar];
  92. y = 0;
  93. w = 80;
  94. UIButton *allBtn = [[UIButton alloc] setxywh];
  95. [allBtn setImage:[UIImage imageNamed:@"news_all"] withTitle:@"全部" forState:UIControlStateNormal];
  96. allBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  97. [allBtn setTitleColor:contentTextColor forState:UIControlStateNormal];
  98. [allBtn setImageEdgeInsets:UIEdgeInsetsMake(0, allBtn.titleLabel.width + allBtn.imageView.width, 0, -allBtn.titleLabel.width - allBtn.imageView.width)];
  99. [allBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -allBtn.imageView.width, 0, allBtn.imageView.width)];
  100. [allBtn addTarget:self action:@selector(openChildMenu) forControlEvents:UIControlEventTouchUpInside];
  101. [childMenuBar addSubview:allBtn];
  102. x += 80;
  103. w = kSize.width - 80;
  104. childMenuLabel = [[UILabel alloc] setxywh];
  105. childMenuLabel.font = [UIFont systemFontOfSize:15];
  106. childMenuLabel.textColor = contentTextColor;
  107. [childMenuBar addSubview:childMenuLabel];
  108. x = 0;
  109. y = 86;
  110. w = kSize.width;
  111. h = kSize.height - y - kNavOffSet;
  112. mainTableView = [[UITableView alloc] setxywh style:UITableViewStylePlain];
  113. mainTableView.delegate = self;
  114. mainTableView.dataSource = self;
  115. mainTableView.tag = 1;
  116. mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  117. [self.view addSubview:mainTableView];
  118. placeHolder = [[HolderView alloc] initWithFrame:mainTableView.frame];
  119. [placeHolder freshBlock:^{
  120. currentPage = 1;
  121. [self getNewsInfoByItemId];
  122. }];
  123. [self.view addSubview:placeHolder];
  124. //子菜单写在这里 在下边只要做位置的变动就好了
  125. x = kSize.width;
  126. childView = [[UIView alloc] setxywh];
  127. childView.backgroundColor = [UIColor colorWithWhite:.1 alpha:.1];
  128. [self.view addSubview:childView];
  129. w = kSize.width/3.0;
  130. childTableView = [[UITableView alloc] initWithFrame:CGRectMake(w, 0, 2*w, h) style:UITableViewStylePlain];
  131. childTableView.delegate = self;
  132. childTableView.dataSource = self;
  133. childTableView.tag = 2;
  134. [childView addSubview:childTableView];
  135. holderLabel = [[UILabel alloc] initWithFrame:CGRectMake(w, 20, 2*w, 50)];
  136. [holderLabel setText:@"暂无数据" Font:20 TextColor:contentTextColor Alignment:NSTextAlignmentCenter];
  137. [childView addSubview:holderLabel];
  138. //默认显示新闻
  139. [self getNewsInfoByItemId];
  140. }
  141. -(void)btnClick:(UIButton *)sender
  142. {
  143. //NSLog(@"tag---->%d",(int)sender.tag);
  144. //在这里把下面处理下
  145. childMenuLabel.text = @"";
  146. //说明屏幕有二级菜单
  147. if (childView.x < 100) {
  148. [UIView animateWithDuration:.5 animations:^{
  149. isChild = NO;
  150. childView.x = kSize.width;
  151. childView.backgroundColor = [UIColor colorWithWhite:.1 alpha:.1];
  152. }];
  153. }
  154. //改变按钮的状态
  155. UIButton *lastBtn = (UIButton *)[supMenuScroll viewWithTag:lastID];
  156. lastBtn.selected = NO;
  157. sender.selected = YES;
  158. lastID = sender.tag;
  159. //移动按钮下的线
  160. NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:sender.titleLabel.font,NSFontAttributeName, nil];
  161. float width= [[superMenu[sender.tag - 1] objectForKey:@"NAME"] sizeWithAttributes:dic].width;
  162. [UIView beginAnimations:nil context:nil];
  163. [UIView setAnimationDuration:.3];
  164. lineLabel.bounds=CGRectMake(0, 0, width, 2);
  165. lineLabel.center=CGPointMake(sender.center.x, lineLabel.center.y);
  166. [UIView commitAnimations];
  167. NSDictionary *menuDic = superMenu[sender.tag - 1];
  168. selectID = menuDic[@"ID"];
  169. placeHolder.hidden = NO;
  170. currentPage = 1;
  171. searchID = selectID;
  172. [self getNewsInfoByItemId];
  173. }
  174. -(void)openChildMenu
  175. {
  176. if (isChild) {
  177. return;
  178. }
  179. isChild = YES;
  180. holderLabel.hidden = NO;
  181. childMenu = [NSArray array];
  182. [childTableView reloadData];
  183. [self getNewsItemByPidAndTypeWithID:selectID];
  184. [UIView animateWithDuration:.5 animations:^{
  185. childView.x = 0;
  186. childView.backgroundColor = [UIColor colorWithWhite:.1 alpha:.8];
  187. }];
  188. }
  189. #pragma mark 异步获取新闻栏目
  190. -(void)getNewsItemByPidAndTypeWithID:(NSString *)pid
  191. {
  192. if (![Util connectedToNetWork]) {
  193. showMsgUnconnect();
  194. return;
  195. }
  196. NSMutableArray *arr=[NSMutableArray array];
  197. [arr addPro:@"pid" Value:pid];
  198. NSString* method = @"getNewsItemByPidAndType";
  199. [MBProgressHUD showLoadToView:self.view];
  200. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  201. [MBProgressHUD hideHUDForView:self.view];
  202. if (!root)
  203. {
  204. [LoadingView showMsg:@"网络请求失败!"];
  205. return;
  206. }
  207. if ([root[@"code"] isEqualToString:@"1"]) {
  208. ShowMsg(root[@"body"]);
  209. return;
  210. }
  211. if ([root[@"body"] count] < 1) {
  212. //ShowMsg(@"暂无数据");
  213. return;
  214. }
  215. if ([pid isEqualToString:@"0"]) {
  216. superMenu = root[@"body"];
  217. [self creatMainUI];
  218. }else{
  219. //将‘全部’按钮插进去
  220. childMenu = root[@"body"];
  221. NSMutableArray *array = [NSMutableArray arrayWithArray:childMenu];
  222. NSDictionary *superData = @{@"ID":selectID,@"NAME":@"全部",@"PID":@"0",@"TYPE":@"0"};
  223. [array insertObject:superData atIndex:0];
  224. childMenu = [NSArray arrayWithArray:array];
  225. if (childMenu.count == 0) {
  226. holderLabel.hidden = NO;
  227. }else{
  228. holderLabel.hidden = YES;
  229. [childTableView reloadData];
  230. }
  231. }
  232. }];
  233. }
  234. #pragma mark 异步获取新闻列表
  235. -(void)getNewsInfoByItemId
  236. {
  237. if (![Util connectedToNetWork]) {
  238. showMsgUnconnect();
  239. return;
  240. }
  241. NSMutableArray *arr=[NSMutableArray array];
  242. [arr addPro:@"itemId" Value:searchID];
  243. [arr addPro:@"isPage" Value:@"1"];
  244. [arr addPro:@"pageSize" Value:@"10"];
  245. [arr addPro:@"currentPage" Value:[NSString stringWithFormat:@"%d",(int)currentPage]];
  246. NSString* method = @"getNewsInfoByItemId";
  247. [MBProgressHUD showLoadToView:self.view];
  248. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  249. [MBProgressHUD hideHUDForView:self.view];
  250. //NSLog(@"getNewsInfoByItemId---%@--->%@",arr,root);
  251. if (!root)
  252. {
  253. [LoadingView showMsg:@"网络请求失败!"];
  254. return;
  255. }
  256. if ([root[@"code"] isEqualToString:@"1"]) {
  257. ShowMsg(root[@"body"]);
  258. }
  259. if ([root[@"body"] count] < 1) {
  260. ShowMsg(@"已加载全部");
  261. return;
  262. }
  263. if (currentPage == 1) {
  264. newsList = [NSMutableArray arrayWithArray:root[@"body"]];
  265. }else{
  266. [newsList addObjectsFromArray:root[@"body"]];
  267. }
  268. currentPage ++;
  269. placeHolder.hidden = YES;
  270. [mainTableView reloadData];
  271. }];
  272. }
  273. #pragma mark maintableView delegate
  274. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  275. {
  276. if (tableView.tag == 1) {
  277. return newsList.count;
  278. }else{
  279. return childMenu.count;
  280. }
  281. }
  282. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  283. {
  284. if (tableView.tag == 2) {
  285. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sysCell"];
  286. if (cell == nil)
  287. {
  288. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"sysCell"];
  289. }
  290. NSDictionary *dic = childMenu[indexPath.row];
  291. cell.textLabel.text = dic[@"NAME"];
  292. return cell;
  293. }
  294. NSDictionary* dic = newsList[indexPath.row];
  295. //NSLog(@"------>%@",dic);
  296. if ([dic[@"ISIMG"] isEqualToString:@"0"]) {
  297. NewListNoImg *cell = [tableView dequeueReusableCellWithIdentifier:@"newNoImg"];
  298. if (cell == nil)
  299. {
  300. NSArray * viewArray = [[NSBundle mainBundle] loadNibNamed:@"NewListNoImg" owner:nil options:nil];
  301. cell = [viewArray firstObject];
  302. }
  303. cell.titleLabel.text = dic[@"TITLE"];
  304. if ([dic[@"LIGHT"] isEqualToString:@"0"]) {
  305. cell.titleLabel.textColor = kTitleColor;
  306. }else{
  307. cell.titleLabel.textColor = [UIColor redColor];
  308. }
  309. cell.contentLabel.text = [NSString stringWithFormat:@" %@",[dic[@"REMARK"] stringByReplacingOccurrencesOfString:@"&amp;" withString:@""]];
  310. cell.dateLabel.text = dic[@"ADDTIME"];
  311. if ([dic[@"ADDTIME"] length] > 10) {
  312. cell.dateLabel.text = [dic[@"ADDTIME"] substringToIndex:10];
  313. }
  314. if ([dic[@"ADDTIME"] isEqualToString:@""]) {
  315. cell.dateLabel.text = @"摘自网络";
  316. }
  317. cell.countLabel.text = [NSString stringWithFormat:@"阅读:%@",[Tools isWan:dic[@"READNUM"]]];
  318. return cell;
  319. }else{
  320. newDetailVC *cell = [tableView dequeueReusableCellWithIdentifier:@"newCell"];
  321. if (cell == nil)
  322. {
  323. NSArray * viewArray = [[NSBundle mainBundle] loadNibNamed:@"newDetailVC" owner:nil options:nil];
  324. cell = [viewArray firstObject];
  325. }
  326. //设置图片大小不变 超出部分截掉
  327. //UIViewContentModeCenter
  328. cell.headImg.contentMode = UIViewContentModeScaleAspectFill;
  329. cell.headImg.clipsToBounds = YES;
  330. //dansonimg
  331. [cell.headImg sd_setImageWithURL:[NSURL URLWithString:dic[@"IMGPATH"]] placeholderImage:[UIImage imageNamed:@"NOIMG.png"]];
  332. //设置标题及标题颜色
  333. cell.titleLabel.text = dic[@"TITLE"];
  334. if ([dic[@"LIGHT"] isEqualToString:@"0"]) {
  335. cell.titleLabel.textColor = kTitleColor;
  336. }else{
  337. cell.titleLabel.textColor = [UIColor redColor];
  338. }
  339. cell.contentLabel.text = [NSString stringWithFormat:@" %@",[dic[@"REMARK"] stringByReplacingOccurrencesOfString:@"&amp;" withString:@""]];
  340. cell.sourceLabel.text = dic[@"ADDTIME"];
  341. if ([dic[@"ADDTIME"] length] > 10) {
  342. cell.sourceLabel.text = [dic[@"ADDTIME"] substringToIndex:10];
  343. }
  344. if ([dic[@"ADDTIME"] isEqualToString:@""]) {
  345. cell.sourceLabel.text = @"摘自网络";
  346. }
  347. cell.readCount.text = [NSString stringWithFormat:@"阅读:%@",[Tools isWan:dic[@"READNUM"]]];
  348. return cell;
  349. }
  350. }
  351. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  352. {
  353. if (tableView.tag == 2) {
  354. return 50;
  355. }
  356. NSDictionary *dic = newsList[indexPath.row];
  357. if ([dic[@"ISIMG"] isEqualToString:@"0"]) {
  358. return 110;
  359. }else{
  360. return 130;
  361. }
  362. }
  363. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  364. {
  365. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  366. if (tableView.tag == 2) {
  367. NSDictionary *dic = childMenu[indexPath.row];
  368. currentPage = 1;
  369. searchID = dic[@"ID"];
  370. childMenuLabel.text = dic[@"NAME"];
  371. if ([dic[@"NAME"] isEqualToString:@"全部"]) {
  372. childMenuLabel.text = @"";
  373. }
  374. [self getNewsInfoByItemId];
  375. [UIView animateWithDuration:.5 animations:^{
  376. isChild = NO;
  377. childView.x = kSize.width;
  378. childView.backgroundColor = [UIColor colorWithWhite:.1 alpha:.1];
  379. }];
  380. return;
  381. }
  382. NSDictionary *dic = newsList[indexPath.row];
  383. NewsDetVC* vc = [[NewsDetVC alloc] init];
  384. vc.preModel = dic;
  385. vc.type = @"3";
  386. [self navPushHideTabbarToVC:vc];
  387. }
  388. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  389. {
  390. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  391. return view;
  392. }
  393. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  394. {
  395. return .1;
  396. }
  397. -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  398. {
  399. //NSLog(@"%d",(int)scrollView.tag);
  400. if (scrollView.tag == 2) {
  401. return;
  402. }
  403. CGPoint off = scrollView.contentOffset;
  404. if (scrollView.contentSize.height - off.y - scrollView.frame.size.height < 1) {
  405. [scrollView setContentOffset:CGPointMake(off.x, off.y - 10) animated:YES];
  406. [self getNewsInfoByItemId];
  407. }
  408. }
  409. - (void)didReceiveMemoryWarning {
  410. [super didReceiveMemoryWarning];
  411. // Dispose of any resources that can be recreated.
  412. }
  413. @end