123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- //
- // WillDoVC.m
- // LNManager
- //
- // Created by EchoShacolee on 2017/4/6.
- // Copyright © 2017年 lee. All rights reserved.
- //
- #import "MessageList.h"
- #import "MessageCell.h"
- #import "MessageDetailVC.h"
- typedef NS_ENUM(NSInteger, MyGetDataType) {
- //正常请求数据
- MyGetDataTypeNomal=0,
- //下拉刷新请求数据
- MyGetDataTypeHeaderRefresh,
- //上拉加载更多请求数据
- MyGetDataTypeFooterRefresh
- };
- @interface MessageList ()<UITableViewDelegate,UITableViewDataSource>
- {
- UITableView *_tableView;
- NSMutableArray *_dataArray;
-
- //记录页书
- NSInteger _currentPageNum;
- //正在加载的状态
- BOOL _IS_LOADING;
- //加载数据的类型
- MyGetDataType _getDataType;
-
- //
- HolderView *_holderV;
-
- //
- NSString *_isRead;
-
- //缓存高度
- NSMutableArray * _heightArr;
- }
- @end
- @implementation MessageList
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.navigationItem.title = @"未读消息";
- self.navigationController.navigationBar.translucent = NO;
-
- _dataArray = [NSMutableArray new];
- _heightArr = [NSMutableArray new];
- _isRead = @"0";
- _currentPageNum = 0;
- _IS_LOADING = NO;
- _getDataType = MyGetDataTypeNomal;
-
- [self myInit];
- [self setRefreshAction];
- [self getData];
- }
- -(void)myInit{
-
- [self goBackByNavigation];
- [self customRightBtn];
-
- //处理底部”上拉加载更多“显示异常(5c,ios10)
- UIView *view = [[UIView alloc]init];
- [self.view addSubview:view];
-
- _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kFrame.size.width, kFrame.size.height - kNavOffSet) style:UITableViewStylePlain];
- _tableView.rowHeight = 70.0f;
- _tableView.delegate = self;
- _tableView.dataSource = self;
- _tableView.backgroundColor = RGB_COLOR(242, 243, 241);
- _tableView.showsVerticalScrollIndicator = NO;
- _tableView.tableFooterView = [UIView new];
- [self.view addSubview:_tableView];
- [_tableView registerNib:[UINib nibWithNibName:@"MessageCell" bundle:nil] forCellReuseIdentifier:@"MessageCellId"];
-
- _holderV = [[HolderView alloc]initWithFrame:_tableView.frame];
- [_holderV freshBlock:^{
- [self headerRefresh];
- }];
- [self.view addSubview:_holderV];
- }
- #pragma mark 导航栏右键
- -(void)customRightBtn{
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
- btn.frame = CGRectMake(0, 0, 80, 44);
- btn.titleLabel.font = [UIFont systemFontOfSize:17];
- [btn setTitle:@"查看已读" forState:UIControlStateNormal];
- [btn setTitleColor:defGreen forState:UIControlStateNormal];
- [btn addTarget:self action:@selector(hasReadClick:) forControlEvents:UIControlEventTouchUpInside];
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
- }
- -(void)hasReadClick:(UIButton *)item{
-
- if ([_isRead isEqualToString:@"0"]) {
- _isRead = @"1";
- [item setTitle:@"查看未读" forState:UIControlStateNormal];
- self.navigationItem.title = @"已读消息";
- }else{
- _isRead = @"0";
- [item setTitle:@"查看已读" forState:UIControlStateNormal];
- self.navigationItem.title = @"未读消息";
- }
-
- [self headerRefresh];
- }
- #pragma mark - 下拉刷新,上拉加载 -
- -(void)setRefreshAction{
-
- // 下拉刷新
- __weak typeof(self) weakSelf = self;
- MJRefreshNormalHeader *mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
- [weakSelf headerRefresh];
- }];
- _tableView.mj_header = mj_header;
-
- MJRefreshBackNormalFooter *mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
- [weakSelf footerRefresh];
- }];
- _tableView.mj_footer = mj_footer;
-
- }
- #pragma mark - 数据加载更多和刷新 -
- -(void)headerRefresh{
- //设置获取数据的方式
- _getDataType=MyGetDataTypeHeaderRefresh;
- //加载数据
- [self getData];
- [_tableView.mj_header endRefreshing];
- }
- -(void)footerRefresh{
- //设置获取数据的方式
- _getDataType=MyGetDataTypeFooterRefresh;
- //加载数据
- [self getData];
- [_tableView.mj_footer endRefreshing];
- }
- #pragma mark tableview代理方法
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return _dataArray.count;
- }
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCellId"];
- cell.dic = _dataArray[indexPath.row];
- return cell;
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- return 70;
-
- // //缓存高度
- // NSNumber *cellHeight = [_heightArr h_safeObjectAtIndex:indexPath.row];
- //
- // if (cellHeight) {
- //
- // return [cellHeight floatValue];
- //
- // }else{
- //
- // NSDictionary * dic = _dataArray[indexPath.row];
- //
- // CGFloat w = _tableView.frame.size.width-60;
- // NSString *str = [NSString stringWithFormat:@"%@",dic[@"MI_CONTENT"]];
- // CGFloat h1 = [str heightForWid:w Font:14] + 5;
- // NSString *str2 = [NSString stringWithFormat:@"%@",dic[@"MI_TITLE"]];
- // CGFloat h2 = [str2 heightForWid:w Font:17] + 5;
- //
- // CGFloat H = 46+h1+h2;//67为其它固定高度
- // [_heightArr addObject:@(H)];
- // return H;
- // }
- }
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- MessageDetailVC *vc = [[MessageDetailVC alloc]init];
- vc.dic = _dataArray[indexPath.row];
- vc.block = ^{
- [self headerRefresh];
- };
- [self navPushHideTabbarToVC:vc];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark 网络请求
- -(void)getData{
-
- //判断当前是否正在加载数据。如果正在加载数据,直接return。
- if (_IS_LOADING) {
- return;
- }
-
- if (![NetManager connectedToNetWork]) {
- showMsgUnconnect();
- return;
- }
-
- _IS_LOADING=YES;
- //获取第一页数据
- NSInteger needLoadPage;
- needLoadPage=_currentPageNum+1;
- if (_getDataType==MyGetDataTypeHeaderRefresh) {
- needLoadPage=1;
- }
-
- NSMutableDictionary *dic = [NSMutableDictionary dictionary];
- [dic setObject:defUser.userDict[@"id"] forKey:@"userId"];
- [dic setObject:_isRead forKey:@"isRead"];
- [dic setObject:@"1" forKey:@"isPage"];
- [dic setObject:@"10" forKey:@"pageSize"];
- [dic setObject:[NSString stringWithFormat:@"%ld",(long)needLoadPage] forKey:@"currentPage"];
-
- NSString *method = @"getUnreadMsgList";
- [MBProgressHUD showLoadToView:self.view];
- [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) {
-
- [MBProgressHUD hideHUDForView:self.view];
-
- _holderV.hidden = NO;
- //设置加载状态
- _IS_LOADING=NO;
-
- if (!root) {
- return;
- }
- if ([root[@"code"] integerValue] == 1) {
- return;
- }
-
- //处理数据
- //如果获取到了数据,而且是下拉刷新,清空数组。
- if (_getDataType==MyGetDataTypeHeaderRefresh){
- [_dataArray removeAllObjects];
- }
- //追加数据
- [_dataArray addObjectsFromArray:root[@"body"]];
- if (_dataArray.count != 0) {
- _holderV.hidden = YES;
- //计数器+1,这里只保证“点击刷新”的时候不会增加页数
- _currentPageNum=needLoadPage;
- }
-
- [_tableView reloadData];
-
- //恢复初始状态。
- _getDataType=MyGetDataTypeNomal;
-
- }];
-
- }
- @end
|