123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- //
- // newsViewController.m
- // ZhongJingLianMeng
- //
- // Created by vinDesel on 2018/11/15.
- // Copyright © 2018年 youke. All rights reserved.
- //
- #import "newsViewController.h"
- #import "newsTableViewCell.h"
- #import "OrderListEmptyView.h"
- #import "HttpRequestManger.h"
- #import "NewsModel.h"
- #import "newsDetailVController.h"
- #import "activityDetailVController.h"
- @interface newsViewController ()<UITableViewDelegate,UITableViewDataSource>
- {
- CGFloat viewHeight;
- }
- @property (strong, nonatomic) UITableView *tableView;
- @property (strong, nonatomic) HttpRequestManger *sessionManager;
- @property (nonatomic, strong) NSMutableArray *listArray;
- @property (nonatomic, strong) NSMutableArray *ListDataSource;
- @property (nonatomic, strong) OrderListEmptyView *collectEmpty;
- @property (nonatomic, copy) NSString *deleteStr;
- @property (strong, nonatomic) UIView *editingView;
- @property (strong, nonatomic) UIButton *commitBtn;
- @property (strong, nonatomic) NSMutableArray *deleteArray;
- //列表上一次的请求参数
- @property (nonatomic, strong) NSDictionary *params;
- //当前页码
- @property (nonatomic, assign) NSInteger Page;
- @property (nonatomic, assign) BOOL isLoading;
- @property (nonatomic, strong) NewsModel *NewsModel;
- @end
- @implementation newsViewController
- - (NSMutableArray *)ListDataSource
- {
- if (!_ListDataSource) {
- _ListDataSource = [NSMutableArray array];
- }
- return _ListDataSource;
- }
- - (NSMutableArray *)listArray
- {
- if (!_listArray) {
- _listArray = [NSMutableArray array];
- }
- return _listArray;
- }
- - (HttpRequestManger*) sessionManager{
- if (!_sessionManager) {
- _sessionManager = [[HttpRequestManger alloc]init];
- }
- return _sessionManager;
- }
- - (OrderListEmptyView *)collectEmpty
- {
- if (!_collectEmpty) {
- _collectEmpty = [[NSBundle mainBundle]loadNibNamed:@"OrderListEmptyView" owner:self options:nil].firstObject;
- _collectEmpty.signLabel.text = @"暂无数据";
- [_collectEmpty.actionButton setTitle:@"立即收藏" forState:UIControlStateNormal];
- _collectEmpty.actionButton.layer.masksToBounds = YES;
- _collectEmpty.actionButton.layer.cornerRadius = 2.0f;
- // [_collectEmpty.actionButton addTarget:self action:@selector(actionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _collectEmpty;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- WS(weakSelf);
- self.navigationView.hidden = NO;
- [self.navigationView setNavigationBackgroundColor:[UIColor colorWithHexValue:0x3C915C]];
- [self.navigationView removeAllLeftButton];
- [self.navigationView addLeftButtonWithImage:[UIImage imageNamed:@"whiteBack"] clickCallBack:^(UIView *view) {
- [weakSelf.navigationController popViewControllerAnimated:YES];
- }];
- [self.navigationView setTitle:@"消息"];
- self.navigationView.titleLabel.textColor=[UIColor whiteColor];
- if (IS_IPHONE_X) {
- viewHeight =88;
- }else{
- viewHeight = 64;
- }
- _deleteArray = [[NSMutableArray alloc] init];
- // _commitBtn.hidden = NO;
- [self layoutSubviews];
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- [self TabSetupRefresh];
- }
- #pragma 列表数据请求
- - (void)TabSetupRefresh
- {
- [self.tableView.mj_header beginRefreshing];
- }
- //加载最新数据
- - (void)TabloadNews
- {
- WS(weakSelf);
- NSMutableDictionary *params = [NSMutableDictionary dictionary];
- params[@"pageNum"] = @"1";
- params[@"pageSize"] = @"10";
- weakSelf.params = params;
- [ProgressHUD show];
- [self.sessionManager sendPostHttpRequestWithUrl:getNewsList params:params bodyBlock:nil progress:nil success:^(NSDictionary *response) {
- [ProgressHUD hide];
- [weakSelf.tableView.mj_header endRefreshing];
- weakSelf.listArray = [response objectForKeySafely:@"selectPushList"];
- weakSelf.ListDataSource = [NewsModel mj_objectArrayWithKeyValuesArray:weakSelf.listArray];
- if (weakSelf.listArray.count == 0) {
- weakSelf.collectEmpty.frame = weakSelf.view.bounds;
- weakSelf.collectEmpty.actionButton.hidden = YES;
- [weakSelf.tableView addSubview:weakSelf.collectEmpty];
- }else{
- [weakSelf.collectEmpty removeFromSuperview];
- }
- [weakSelf.tableView reloadData];
- weakSelf.Page = 1;
- } failure:^(NSError *error) {
- [ProgressHUD hide];
- [weakSelf.tableView.mj_header endRefreshing];
- }];
- }
- //加载更多
- - (void)TabloadMoreTopics
- {
- WS(weakSelf);
- _isLoading = YES;
- self.Page ++;
- NSNumber *current = [NSNumber numberWithInt:(int)self.Page];
- NSMutableDictionary *params = [NSMutableDictionary dictionary];
- params[@"pageNum"] = current;
- params[@"pageSize"] = @"10";
- [ProgressHUD show];
- weakSelf.params = params;
- [self.sessionManager sendPostHttpRequestWithUrl:getNewsList params:params bodyBlock:nil progress:nil success:^(NSDictionary *response) {
- [ProgressHUD hide];
- [weakSelf.tableView.mj_footer endRefreshing];
- if (weakSelf.params != params) return;
- weakSelf.listArray = [response objectForKeySafely:@"selectPushList"];
- if (weakSelf.listArray.count > 0) {
- NSArray *array = [NewsModel mj_objectArrayWithKeyValuesArray:weakSelf.listArray];
- [weakSelf.ListDataSource addObjectsFromArray:array];
- }else{
- if (weakSelf.Page > 1) {
- weakSelf.Page--;
- [ToastView showToast:@"暂无更多数据"];
- }
- }
- [weakSelf.tableView reloadData];
- } failure:^(NSError *error) {
- [ProgressHUD hide];
- [weakSelf.tableView.mj_footer endRefreshing];
- }];
- }
- - (UITableView *)tableView
- {
- if (!_tableView) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewHeight, SCREEN_WIDTH, SCREEN_HEIGHT - viewHeight)];
- _tableView.dataSource = self;
- _tableView.delegate = self;
- _tableView.backgroundColor = RGBColor(247, 248, 250);
- _tableView.tableFooterView = [[UIView alloc] init];
- _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- //去掉多余的分割线
- WS(weakSelf);
- weakSelf.tableView.tableFooterView = [[UIView alloc]init];
- // 下拉刷新
- weakSelf.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(TabloadNews)];
- weakSelf.tableView.mj_header.automaticallyChangeAlpha = YES;
- // 上拉加载
- weakSelf.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(TabloadMoreTopics)];
- if (@available(iOS 11.0, *)) {
- _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- }else {
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
- }
- return _tableView;
- }
- - (UIView *)editingView
- {
- if (!_editingView) {
- _editingView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 50)];
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.backgroundColor = [UIColor colorWithHexValue:0xCBCFCC];
- [button setTitle:@"取消" forState:UIControlStateNormal];
- [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [button addTarget:self action:@selector(p__buttonClick:) forControlEvents:UIControlEventTouchUpInside];
- button.frame = CGRectMake(0, 0, SCREEN_WIDTH / 2, 50);
- UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
- button1.backgroundColor = [UIColor colorWithHexValue:0x3C915C];
- [button1 setTitle:@"删除" forState:UIControlStateNormal];
- [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [button1 addTarget:self action:@selector(p__buttonClick:) forControlEvents:UIControlEventTouchUpInside];
- button1.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2, 50);
- [_editingView addSubview:button];
- [_editingView addSubview:button1];
- }
- return _editingView;
- }
- //删除消息
- - (void)p__buttonClick:(UIButton *)sender{
- if ([[sender titleForState:UIControlStateNormal] isEqualToString:@"删除"]) {
- NSMutableDictionary *params = [NSMutableDictionary dictionary];
- _deleteStr = [_deleteArray componentsJoinedByString:@","];
- params[@"id"] = _deleteStr;
- [ProgressHUD show];
- [self.sessionManager sendPostHttpRequestWithUrl:delateNews params:params bodyBlock:nil progress:nil success:^(NSDictionary *response) {
- [ProgressHUD hide];
- [ToastView showToast:@"删除成功"];
- [self TabSetupRefresh];
- [_deleteArray removeAllObjects];
- [self.tableView setEditing:NO animated:YES];
- [self showEitingView:NO];
- _commitBtn.hidden = NO;
- } failure:^(NSError *error) {
- [ProgressHUD hide];
- [ToastView showToast:@"删除失败"];
- }];
- }else if ([[sender titleForState:UIControlStateNormal] isEqualToString:@"取消"]){
- [self.tableView setEditing:NO animated:YES];
- [self showEitingView:NO];
- [_deleteArray removeAllObjects];
- _commitBtn.hidden = NO;
- }
- }
- - (void)showEitingView:(BOOL)isShow
- {
- [UIView animateWithDuration:0.3 animations:^{
- self.editingView.frame = CGRectMake(0, isShow ? SCREEN_HEIGHT - 50 : SCREEN_HEIGHT, SCREEN_WIDTH, 50);
- }];
- }
- - (void)layoutSubviews
- {
- _commitBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
- [_commitBtn setTitle:@"编辑" forState:UIControlStateNormal];
- [_commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- _commitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
- _commitBtn.hidden = NO;
- [_commitBtn addTarget:self action:@selector(commitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
- [self.navigationView addRightView:_commitBtn clickCallback:nil];
-
- [self.view addSubview:self.tableView];
- [self.view addSubview:self.editingView];
- }
- - (void)commitBtnClick:(UIButton *)sender
- {
- if ([_commitBtn.titleLabel.text isEqualToString:@"编辑"]) {
- if (self.ListDataSource.count == 0) {
- return;
- }
- // [_commitBtn setTitle:@"完成" forState:UIControlStateNormal];
- [self.tableView setEditing:YES animated:YES];
- [self showEitingView:YES];
- _commitBtn.hidden = YES;
- }
- // else{
- // [_commitBtn setTitle:@"编辑" forState:UIControlStateNormal];
- // [self.tableView setEditing:NO animated:YES];
- // [self showEitingView:NO];
- // [_deleteArray removeAllObjects];
- // }
- }
- #pragma mark -- UITabelViewDelegate And DataSource
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return self.ListDataSource.count;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewAutomaticDimension;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- _NewsModel = self.ListDataSource[indexPath.row];
- newsTableViewCell *cell = [newsTableViewCell newsTableViewCellWithTableView:tableView];
- cell.selectionStyle = UITableViewCellEditingStyleNone;
- cell.timeLabel.text = _NewsModel.create_time;
- cell.titleLabel.text = @"系统消息";
- cell.contentLabel.text = _NewsModel.title;
- if ([_NewsModel.status isEqualToString:@"1"]) {
- cell.redPoint.hidden = YES;
- }else if ([_NewsModel.status isEqualToString:@"0"]){
- cell.redPoint.hidden = NO;
- }
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return 200;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- _NewsModel = self.ListDataSource[indexPath.row];
- if (tableView.isEditing)
- {
- [_deleteArray addObject:_NewsModel.updateId];
- return;
- }else{
- // [ToastView showToast:@"跳转详情"];
- [self readNewsByNewsId:_NewsModel.updateId];
- if ([_NewsModel.type isEqualToString:@"0"]) {
- newsDetailVController *newsDetail = [[newsDetailVController alloc]init];
- newsDetail.newsId = _NewsModel.message_id;
- newsDetail.titleStr = _NewsModel.title;
- newsDetail.shareTitle = _NewsModel.title;
- newsDetail.typeStr = _NewsModel.type;
- newsDetail.ImgUrl = _NewsModel.img_path;
-
- newsDetail.index =@"0";
- newsDetail.urlStr = [NSString stringWithFormat:@"%@&move=ios",_NewsModel.base_url_ios];
- newsDetail.shareUrl = [NSString stringWithFormat:@"%@",_NewsModel.base_url_ios];
- [self.navigationController pushViewController:newsDetail animated:YES];
- }else if ([_NewsModel.type isEqualToString:@"1"]){
- activityDetailVController *activityDetail = [[activityDetailVController alloc]init];
- activityDetail.newsId = _NewsModel.message_id;
- activityDetail.titleStr = _NewsModel.title;
- activityDetail.shareTitle = _NewsModel.title;
- activityDetail.ImgUrl = _NewsModel.img_path;
- activityDetail.index =@"0";
- activityDetail.activityStatus = _NewsModel.activity;
- activityDetail.urlStr = [NSString stringWithFormat:@"%@&move=ios",_NewsModel.base_url_ios];
- activityDetail.shareUrl = [NSString stringWithFormat:@"%@",_NewsModel.base_url_ios];
- [self.navigationController pushViewController:activityDetail animated:YES];
- }else if ([_NewsModel.type isEqualToString:@"2"]){
- newsDetailVController *newsDetail = [[newsDetailVController alloc]init];
- newsDetail.newsId = _NewsModel.message_id;
- newsDetail.titleStr = _NewsModel.title;
- newsDetail.shareTitle = _NewsModel.title;
- newsDetail.typeStr = _NewsModel.type;
- newsDetail.ImgUrl = _NewsModel.img_path;
- newsDetail.index =@"0";
- newsDetail.urlStr = [NSString stringWithFormat:@"%@",_NewsModel.base_url_ios];
- newsDetail.shareUrl = [NSString stringWithFormat:@"%@",_NewsModel.base_url_ios];
- [self.navigationController pushViewController:newsDetail animated:YES];
- }
- }
- }
- - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
- return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
- }
- - (void)readNewsByNewsId:(NSString *)NewsId
- {
- NSMutableDictionary *params = [NSMutableDictionary dictionary];
- params[@"id"] = NewsId;
- [ProgressHUD show];
- [self.sessionManager sendPostHttpRequestWithUrl:readNews params:params bodyBlock:nil progress:nil success:^(NSDictionary *response) {
- [ProgressHUD hide];
- [self TabSetupRefresh];
- } failure:^(NSError *error) {
- [ProgressHUD hide];
- }];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)dealloc {
- if (_sessionManager) {
- [_sessionManager cancelAllTask];
- }
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- DebugLog(@"%@没有造成循环引用", [self class]);
- }
- @end
|