newsViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // newsViewController.m
  3. // ZhongJingLianMeng
  4. //
  5. // Created by vinDesel on 2018/11/15.
  6. // Copyright © 2018年 youke. All rights reserved.
  7. //
  8. #import "newsViewController.h"
  9. #import "newsTableViewCell.h"
  10. #import "OrderListEmptyView.h"
  11. #import "HttpRequestManger.h"
  12. #import "NewsModel.h"
  13. #import "newsDetailVController.h"
  14. #import "activityDetailVController.h"
  15. @interface newsViewController ()<UITableViewDelegate,UITableViewDataSource>
  16. {
  17. CGFloat viewHeight;
  18. }
  19. @property (strong, nonatomic) UITableView *tableView;
  20. @property (strong, nonatomic) HttpRequestManger *sessionManager;
  21. @property (nonatomic, strong) NSMutableArray *listArray;
  22. @property (nonatomic, strong) NSMutableArray *ListDataSource;
  23. @property (nonatomic, strong) OrderListEmptyView *collectEmpty;
  24. @property (nonatomic, copy) NSString *deleteStr;
  25. @property (strong, nonatomic) UIView *editingView;
  26. @property (strong, nonatomic) UIButton *commitBtn;
  27. @property (strong, nonatomic) NSMutableArray *deleteArray;
  28. //列表上一次的请求参数
  29. @property (nonatomic, strong) NSDictionary *params;
  30. //当前页码
  31. @property (nonatomic, assign) NSInteger Page;
  32. @property (nonatomic, assign) BOOL isLoading;
  33. @property (nonatomic, strong) NewsModel *NewsModel;
  34. @end
  35. @implementation newsViewController
  36. - (NSMutableArray *)ListDataSource
  37. {
  38. if (!_ListDataSource) {
  39. _ListDataSource = [NSMutableArray array];
  40. }
  41. return _ListDataSource;
  42. }
  43. - (NSMutableArray *)listArray
  44. {
  45. if (!_listArray) {
  46. _listArray = [NSMutableArray array];
  47. }
  48. return _listArray;
  49. }
  50. - (HttpRequestManger*) sessionManager{
  51. if (!_sessionManager) {
  52. _sessionManager = [[HttpRequestManger alloc]init];
  53. }
  54. return _sessionManager;
  55. }
  56. - (OrderListEmptyView *)collectEmpty
  57. {
  58. if (!_collectEmpty) {
  59. _collectEmpty = [[NSBundle mainBundle]loadNibNamed:@"OrderListEmptyView" owner:self options:nil].firstObject;
  60. _collectEmpty.signLabel.text = @"暂无数据";
  61. [_collectEmpty.actionButton setTitle:@"立即收藏" forState:UIControlStateNormal];
  62. _collectEmpty.actionButton.layer.masksToBounds = YES;
  63. _collectEmpty.actionButton.layer.cornerRadius = 2.0f;
  64. // [_collectEmpty.actionButton addTarget:self action:@selector(actionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  65. }
  66. return _collectEmpty;
  67. }
  68. - (void)viewDidLoad {
  69. [super viewDidLoad];
  70. // Do any additional setup after loading the view.
  71. WS(weakSelf);
  72. self.navigationView.hidden = NO;
  73. [self.navigationView setNavigationBackgroundColor:[UIColor colorWithHexValue:0x3C915C]];
  74. [self.navigationView removeAllLeftButton];
  75. [self.navigationView addLeftButtonWithImage:[UIImage imageNamed:@"whiteBack"] clickCallBack:^(UIView *view) {
  76. [weakSelf.navigationController popViewControllerAnimated:YES];
  77. }];
  78. [self.navigationView setTitle:@"消息"];
  79. self.navigationView.titleLabel.textColor=[UIColor whiteColor];
  80. if (IS_IPHONE_X) {
  81. viewHeight =88;
  82. }else{
  83. viewHeight = 64;
  84. }
  85. _deleteArray = [[NSMutableArray alloc] init];
  86. // _commitBtn.hidden = NO;
  87. [self layoutSubviews];
  88. }
  89. - (void)viewWillAppear:(BOOL)animated
  90. {
  91. [super viewWillAppear:animated];
  92. [self TabSetupRefresh];
  93. }
  94. #pragma 列表数据请求
  95. - (void)TabSetupRefresh
  96. {
  97. [self.tableView.mj_header beginRefreshing];
  98. }
  99. //加载最新数据
  100. - (void)TabloadNews
  101. {
  102. WS(weakSelf);
  103. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  104. params[@"pageNum"] = @"1";
  105. params[@"pageSize"] = @"10";
  106. weakSelf.params = params;
  107. [ProgressHUD show];
  108. [self.sessionManager sendPostHttpRequestWithUrl:getNewsList params:params bodyBlock:nil progress:nil success:^(NSDictionary *response) {
  109. [ProgressHUD hide];
  110. [weakSelf.tableView.mj_header endRefreshing];
  111. weakSelf.listArray = [response objectForKeySafely:@"selectPushList"];
  112. weakSelf.ListDataSource = [NewsModel mj_objectArrayWithKeyValuesArray:weakSelf.listArray];
  113. if (weakSelf.listArray.count == 0) {
  114. weakSelf.collectEmpty.frame = weakSelf.view.bounds;
  115. weakSelf.collectEmpty.actionButton.hidden = YES;
  116. [weakSelf.tableView addSubview:weakSelf.collectEmpty];
  117. }else{
  118. [weakSelf.collectEmpty removeFromSuperview];
  119. }
  120. [weakSelf.tableView reloadData];
  121. weakSelf.Page = 1;
  122. } failure:^(NSError *error) {
  123. [ProgressHUD hide];
  124. [weakSelf.tableView.mj_header endRefreshing];
  125. }];
  126. }
  127. //加载更多
  128. - (void)TabloadMoreTopics
  129. {
  130. WS(weakSelf);
  131. _isLoading = YES;
  132. self.Page ++;
  133. NSNumber *current = [NSNumber numberWithInt:(int)self.Page];
  134. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  135. params[@"pageNum"] = current;
  136. params[@"pageSize"] = @"10";
  137. [ProgressHUD show];
  138. weakSelf.params = params;
  139. [self.sessionManager sendPostHttpRequestWithUrl:getNewsList params:params bodyBlock:nil progress:nil success:^(NSDictionary *response) {
  140. [ProgressHUD hide];
  141. [weakSelf.tableView.mj_footer endRefreshing];
  142. if (weakSelf.params != params) return;
  143. weakSelf.listArray = [response objectForKeySafely:@"selectPushList"];
  144. if (weakSelf.listArray.count > 0) {
  145. NSArray *array = [NewsModel mj_objectArrayWithKeyValuesArray:weakSelf.listArray];
  146. [weakSelf.ListDataSource addObjectsFromArray:array];
  147. }else{
  148. if (weakSelf.Page > 1) {
  149. weakSelf.Page--;
  150. [ToastView showToast:@"暂无更多数据"];
  151. }
  152. }
  153. [weakSelf.tableView reloadData];
  154. } failure:^(NSError *error) {
  155. [ProgressHUD hide];
  156. [weakSelf.tableView.mj_footer endRefreshing];
  157. }];
  158. }
  159. - (UITableView *)tableView
  160. {
  161. if (!_tableView) {
  162. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewHeight, SCREEN_WIDTH, SCREEN_HEIGHT - viewHeight)];
  163. _tableView.dataSource = self;
  164. _tableView.delegate = self;
  165. _tableView.backgroundColor = RGBColor(247, 248, 250);
  166. _tableView.tableFooterView = [[UIView alloc] init];
  167. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  168. //去掉多余的分割线
  169. WS(weakSelf);
  170. weakSelf.tableView.tableFooterView = [[UIView alloc]init];
  171. // 下拉刷新
  172. weakSelf.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(TabloadNews)];
  173. weakSelf.tableView.mj_header.automaticallyChangeAlpha = YES;
  174. // 上拉加载
  175. weakSelf.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(TabloadMoreTopics)];
  176. if (@available(iOS 11.0, *)) {
  177. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  178. }else {
  179. self.automaticallyAdjustsScrollViewInsets = NO;
  180. }
  181. }
  182. return _tableView;
  183. }
  184. - (UIView *)editingView
  185. {
  186. if (!_editingView) {
  187. _editingView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 50)];
  188. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  189. button.backgroundColor = [UIColor colorWithHexValue:0xCBCFCC];
  190. [button setTitle:@"取消" forState:UIControlStateNormal];
  191. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  192. [button addTarget:self action:@selector(p__buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  193. button.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, 50);
  194. UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
  195. button1.backgroundColor = [UIColor colorWithHexValue:0x3C915C];
  196. [button1 setTitle:@"删除" forState:UIControlStateNormal];
  197. [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  198. [button1 addTarget:self action:@selector(p__buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  199. button1.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, 50);
  200. [_editingView addSubview:button];
  201. [_editingView addSubview:button1];
  202. }
  203. return _editingView;
  204. }
  205. //删除消息
  206. - (void)p__buttonClick:(UIButton *)sender{
  207. if ([[sender titleForState:UIControlStateNormal] isEqualToString:@"删除"]) {
  208. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  209. _deleteStr = [_deleteArray componentsJoinedByString:@","];
  210. params[@"id"] = _deleteStr;
  211. [ProgressHUD show];
  212. [self.sessionManager sendPostHttpRequestWithUrl:delateNews params:params bodyBlock:nil progress:nil success:^(NSDictionary *response) {
  213. [ProgressHUD hide];
  214. [ToastView showToast:@"删除成功"];
  215. [self TabSetupRefresh];
  216. [_deleteArray removeAllObjects];
  217. [self.tableView setEditing:NO animated:YES];
  218. [self showEitingView:NO];
  219. _commitBtn.hidden = NO;
  220. } failure:^(NSError *error) {
  221. [ProgressHUD hide];
  222. [ToastView showToast:@"删除失败"];
  223. }];
  224. }else if ([[sender titleForState:UIControlStateNormal] isEqualToString:@"取消"]){
  225. [self.tableView setEditing:NO animated:YES];
  226. [self showEitingView:NO];
  227. [_deleteArray removeAllObjects];
  228. _commitBtn.hidden = NO;
  229. }
  230. }
  231. - (void)showEitingView:(BOOL)isShow
  232. {
  233. [UIView animateWithDuration:0.3 animations:^{
  234. self.editingView.frame = CGRectMake(0, isShow ? SCREEN_HEIGHT - 50 : SCREEN_HEIGHT, SCREEN_WIDTH, 50);
  235. }];
  236. }
  237. - (void)layoutSubviews
  238. {
  239. _commitBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
  240. [_commitBtn setTitle:@"编辑" forState:UIControlStateNormal];
  241. [_commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  242. _commitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  243. _commitBtn.hidden = NO;
  244. [_commitBtn addTarget:self action:@selector(commitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  245. [self.navigationView addRightView:_commitBtn clickCallback:nil];
  246. [self.view addSubview:self.tableView];
  247. [self.view addSubview:self.editingView];
  248. }
  249. - (void)commitBtnClick:(UIButton *)sender
  250. {
  251. if ([_commitBtn.titleLabel.text isEqualToString:@"编辑"]) {
  252. if (self.ListDataSource.count == 0) {
  253. return;
  254. }
  255. // [_commitBtn setTitle:@"完成" forState:UIControlStateNormal];
  256. [self.tableView setEditing:YES animated:YES];
  257. [self showEitingView:YES];
  258. _commitBtn.hidden = YES;
  259. }
  260. // else{
  261. // [_commitBtn setTitle:@"编辑" forState:UIControlStateNormal];
  262. // [self.tableView setEditing:NO animated:YES];
  263. // [self showEitingView:NO];
  264. // [_deleteArray removeAllObjects];
  265. // }
  266. }
  267. #pragma mark -- UITabelViewDelegate And DataSource
  268. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  269. {
  270. return 1;
  271. }
  272. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  273. {
  274. return self.ListDataSource.count;
  275. }
  276. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  277. {
  278. return UITableViewAutomaticDimension;
  279. }
  280. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  281. {
  282. _NewsModel = self.ListDataSource[indexPath.row];
  283. newsTableViewCell *cell = [newsTableViewCell newsTableViewCellWithTableView:tableView];
  284. cell.selectionStyle = UITableViewCellEditingStyleNone;
  285. cell.timeLabel.text = _NewsModel.create_time;
  286. cell.titleLabel.text = @"系统消息";
  287. cell.contentLabel.text = _NewsModel.title;
  288. if ([_NewsModel.status isEqualToString:@"1"]) {
  289. cell.redPoint.hidden = YES;
  290. }else if ([_NewsModel.status isEqualToString:@"0"]){
  291. cell.redPoint.hidden = NO;
  292. }
  293. return cell;
  294. }
  295. - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
  296. {
  297. return 200;
  298. }
  299. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  300. {
  301. _NewsModel = self.ListDataSource[indexPath.row];
  302. if (tableView.isEditing)
  303. {
  304. [_deleteArray addObject:_NewsModel.updateId];
  305. return;
  306. }else{
  307. // [ToastView showToast:@"跳转详情"];
  308. [self readNewsByNewsId:_NewsModel.updateId];
  309. if ([_NewsModel.type isEqualToString:@"0"]) {
  310. newsDetailVController *newsDetail = [[newsDetailVController alloc]init];
  311. newsDetail.newsId = _NewsModel.message_id;
  312. newsDetail.titleStr = _NewsModel.title;
  313. newsDetail.shareTitle = _NewsModel.title;
  314. newsDetail.typeStr = _NewsModel.type;
  315. newsDetail.ImgUrl = _NewsModel.img_path;
  316. newsDetail.index =@"0";
  317. newsDetail.urlStr = [NSString stringWithFormat:@"%@&move=ios",_NewsModel.base_url_ios];
  318. newsDetail.shareUrl = [NSString stringWithFormat:@"%@",_NewsModel.base_url_ios];
  319. [self.navigationController pushViewController:newsDetail animated:YES];
  320. }else if ([_NewsModel.type isEqualToString:@"1"]){
  321. activityDetailVController *activityDetail = [[activityDetailVController alloc]init];
  322. activityDetail.newsId = _NewsModel.message_id;
  323. activityDetail.titleStr = _NewsModel.title;
  324. activityDetail.shareTitle = _NewsModel.title;
  325. activityDetail.ImgUrl = _NewsModel.img_path;
  326. activityDetail.index =@"0";
  327. activityDetail.activityStatus = _NewsModel.activity;
  328. activityDetail.urlStr = [NSString stringWithFormat:@"%@&move=ios",_NewsModel.base_url_ios];
  329. activityDetail.shareUrl = [NSString stringWithFormat:@"%@",_NewsModel.base_url_ios];
  330. [self.navigationController pushViewController:activityDetail animated:YES];
  331. }else if ([_NewsModel.type isEqualToString:@"2"]){
  332. newsDetailVController *newsDetail = [[newsDetailVController alloc]init];
  333. newsDetail.newsId = _NewsModel.message_id;
  334. newsDetail.titleStr = _NewsModel.title;
  335. newsDetail.shareTitle = _NewsModel.title;
  336. newsDetail.typeStr = _NewsModel.type;
  337. newsDetail.ImgUrl = _NewsModel.img_path;
  338. newsDetail.index =@"0";
  339. newsDetail.urlStr = [NSString stringWithFormat:@"%@",_NewsModel.base_url_ios];
  340. newsDetail.shareUrl = [NSString stringWithFormat:@"%@",_NewsModel.base_url_ios];
  341. [self.navigationController pushViewController:newsDetail animated:YES];
  342. }
  343. }
  344. }
  345. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
  346. return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
  347. }
  348. - (void)readNewsByNewsId:(NSString *)NewsId
  349. {
  350. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  351. params[@"id"] = NewsId;
  352. [ProgressHUD show];
  353. [self.sessionManager sendPostHttpRequestWithUrl:readNews params:params bodyBlock:nil progress:nil success:^(NSDictionary *response) {
  354. [ProgressHUD hide];
  355. [self TabSetupRefresh];
  356. } failure:^(NSError *error) {
  357. [ProgressHUD hide];
  358. }];
  359. }
  360. - (void)didReceiveMemoryWarning {
  361. [super didReceiveMemoryWarning];
  362. // Dispose of any resources that can be recreated.
  363. }
  364. - (void)dealloc {
  365. if (_sessionManager) {
  366. [_sessionManager cancelAllTask];
  367. }
  368. [[NSNotificationCenter defaultCenter] removeObserver:self];
  369. DebugLog(@"%@没有造成循环引用", [self class]);
  370. }
  371. @end