// // KDPNoticeViewController.m // KuDianProject // // Created by admin on 2019/7/11. // Copyright © 2019 KDP. All rights reserved. // #import "KDPNoticeViewController.h" #import "KDPNoticeModel.h" #import "KDPMessageTableViewCell.h" @interface KDPNoticeViewController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataSource; @property (nonatomic, assign) NSInteger page; @property (nonatomic, strong) UIButton *clearBtn; @end @implementation KDPNoticeViewController -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.tabBarController.tabBar.hidden=YES; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setUpNav]; self.view.backgroundColor = [UIColor colorWithHex:0xF2F2F2]; [self.view addSubview:self.tableView]; } - (void)setUpNav{ self.navBar.navTitleLabel.text = @"我的消息"; [self.navBar addleftReturnButton:self selector:@selector(backAction)]; UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [clearBtn setImage:[UIImage imageNamed:@"clear_icon"] forState:UIControlStateNormal]; clearBtn.frame = CGRectMake(SCREEN_WIDTH-17-16, KDNavBarHeight-16-17, 17, 17); [clearBtn addTarget:self action:@selector(clearAction) forControlEvents:UIControlEventTouchUpInside]; clearBtn.hidden=YES; [self.navBar addSubview:clearBtn]; self.clearBtn = clearBtn; } - (void)backAction{ if (self.presentingViewController) { [self dismissViewControllerAnimated:YES completion:nil]; } else{ [self.navigationController popViewControllerAnimated:YES]; } } - (void)clearAction{ NSString *clearUrl = [NSString stringWithFormat:@"%@api/message_push/allMessageRead",KDURL]; [KDPNetworkRequestHTTP postURL:clearUrl params:nil success:^(id _Nonnull json) { if ([json[@"flag"]intValue] == 1) { for (KDPNoticeModel *model in self.dataSource) { model.is_view = @"1"; } [MBProgressHUD showMessage:@"全部已读"]; [self.tableView reloadData]; } } failure:^(NSError * _Nonnull error) { }]; } - (void)requestMessageData{ NSDictionary *params = @{@"page":@(self.page)}; NSString *messageUrl = [NSString stringWithFormat:@"%@api/message_push/messagePushList",KDURL]; [LoadingView show]; [KDPNetworkRequestHTTP postURL:messageUrl params:params success:^(id _Nonnull json) { [LoadingView dismiss]; NSArray *list = [NSArray yy_modelArrayWithClass:[KDPNoticeModel class] json:json[@"data"]]; if (self.page == 1) { [self.dataSource removeAllObjects]; } if (list.count>0) { [self.dataSource addObjectsFromArray:list]; [self.tableView.mj_footer endRefreshing]; [self.tableView.mj_header endRefreshing]; self.clearBtn.hidden = NO; }else { self.clearBtn.hidden = YES; [self.tableView.mj_header endRefreshing]; [self.tableView.mj_footer endRefreshingWithNoMoreData]; } [LoadingView dismiss]; [self.tableView reloadData]; } failure:^(NSError * _Nonnull error) { [LoadingView dismiss]; [self.tableView.mj_header endRefreshing]; [self.tableView.mj_footer endRefreshing]; }]; } - (UITableView *)tableView{ if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight) style:UITableViewStylePlain]; _tableView.showsVerticalScrollIndicator = NO; _tableView.showsHorizontalScrollIndicator = NO; _tableView.rowHeight = 121; _tableView.emptyDataSetDelegate = self; _tableView.emptyDataSetSource = self; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSelectionStyleNone; _tableView.backgroundColor = [UIColor clearColor]; _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero]; _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; [_tableView registerClass:[KDPMessageTableViewCell class] forCellReuseIdentifier:NSStringFromClass([KDPMessageTableViewCell class])]; _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ self.page = 1; [self requestMessageData]; }]; _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ self.page ++; [self requestMessageData]; }]; [_tableView.mj_header beginRefreshing]; } return _tableView; } - (NSMutableArray *)dataSource{ if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ KDPMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([KDPMessageTableViewCell class])]; [cell configWithViewModel:self.dataSource[indexPath.row] indexpath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataSource.count; } - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{ return [UIImage imageNamed:@"no_order"]; } - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{ return YES; } - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{ return [[NSAttributedString alloc] initWithString:@"还没有记录" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333],NSFontAttributeName:FONT_SYS(12)}]; } - (CGFloat )spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{ return 30; } @end