CNMyAnnouncementsViewController.m 9.34 KB
//
//  CNMyAnnouncementsViewController.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/9/20.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "CNMyAnnouncementsViewController.h"
#import "CNMyAnnouncementsTableViewCell.h"
#import "CNMyAnnouncementsModel.h"

@interface CNMyAnnouncementsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *data;


@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, assign) BOOL isRefreshing;

@end

@implementation CNMyAnnouncementsViewController
static NSString *myAnnouncementsTableViewCell = @"CNMyAnnouncementsTableViewCell";

#pragma mark - Data
- (void)loadData{
    NSString *url = @"http://app.phonemovie.cnlive.com/gxb/api/cataloglatest";
    NSLog(@"%@",url);
    NSString *josnParameters = [NSString stringWithFormat:@"{\"plat\":\"i\",\"version\":\"5.0\",\"appid\":\"gxb\",\"categoryId\":\"wz/fbhgg/\",\"page\":\"%ld\",\"pageSize\":\"%@\"}",(long)self.pageNo,@"15"];
    NSLog(@"%@",josnParameters);
    NSDictionary *parameters = @{@"params":josnParameters};
    
    if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus]) {
        [AlertViewTool showHUDAddedToView:self.view text:CustomStr(@"NetworkiInterruption")];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        self->_isRefreshing = NO;
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [XWHTTPTool POST:url params:parameters success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        // 请求成功,解析数据
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:json];

        if ([dic[@"errorCode"] isEqualToString:@"0"]) {
//            if ([dic[@"programs"] isKindOfClass:[NSDictionary class]] && [dic[@"programs"] count]) {
//                NSDictionary *dics = dic[@"programs"];
//                [CNMyAnnouncementsModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
//                    return @{ @"ID" : @"id" ,@"Description" : @"description" };
//                }];
//                [self.data addObject:[CNMyAnnouncementsModel mj_objectWithKeyValues:dics]];
//            }
            
            if ([dic[@"programs"] isKindOfClass:[NSArray class]]) {
                NSArray *lists = dic[@"programs"];
                if(self->_pageNo == 1&&lists.count > 0){
                    [self.data removeAllObjects];
                }
                self->_pageNo++;

                for (NSDictionary *dics in lists) {
                    [CNMyAnnouncementsModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                        return @{ @"ID" : @"id" ,@"Description" : @"description" };
                    }];
                    NSLog(@"%@",dics);
                    [self.data addObject:[CNMyAnnouncementsModel mj_objectWithKeyValues:dics]];
                    
                }
                
            }
            
        } else {
            [AlertViewTool showHUDAddedToView:self.view text:dic[@"errorMessage"]];

        }
        [self.tableView.mj_header endRefreshing];

        if ([dic[@"nextCursor"] isEqualToString:@"0"]) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            
        } else {
            [self.tableView.mj_footer endRefreshing];
            
        }
        [self.tableView reloadData];
        self->_isRefreshing = NO;

    } failure:^(NSError *error) {
        [AlertViewTool showHUDAddedToView:self.view text:CustomStr(@"ServerBusy")];
        self->_isRefreshing = NO;
        [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];
}

- (void)loadUpData{
    _pageNo = 1;
    if(!_isRefreshing){
        _isRefreshing = YES;
        [self loadData];
    }
}
- (void)loadMoreData{
    if(!_isRefreshing){
        _isRefreshing = YES;
        [self loadData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _pageNo = 1;
    _isRefreshing = NO;
    [self loadData];

    [self setUpNavigationBar];
    [self.view addSubview:self.tableView];
    
}
#pragma mark - UI
- (void)setUpNavigationBar{
    
    self.navigationController.navigationBar.barTintColor = Color_Blue;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    //左
    UIButton *headerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 44)];
    headerButton.layer.cornerRadius = 2.0;
    headerButton.layer.masksToBounds = true;
    [headerButton setImage:[UIImage imageNamed:@"cnlive_white_back"] forState:UIControlStateNormal];
    [headerButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:headerButton];
    
    self.navigationItem.title = self.navTitle;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CNMyAnnouncementsModel *model = self.data[indexPath.row];
    return model.titleHeight+40;

}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    CNMyAnnouncementsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myAnnouncementsTableViewCell];
    if (!cell) {
        cell = [[CNMyAnnouncementsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myAnnouncementsTableViewCell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.data[indexPath.row];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CNMyAnnouncementsModel *model = self.data[indexPath.row];
    NSDictionary *dic = [model toDictionary];
    
    OldArticleDetailViewController *vc = [[OldArticleDetailViewController alloc] initWithDictionary:dic];
    vc.title = dic[@"cmsColumnName"];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

#pragma mark - Action

#pragma mark - Lazy Laoding
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, MainScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        
        [_tableView registerClass:[CNMyAnnouncementsTableViewCell class] forCellReuseIdentifier:myAnnouncementsTableViewCell];
        //  设置上拉加载
        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadUpData)];
        NSMutableArray *refreshingImages = [NSMutableArray array];
        for (NSUInteger i = 0; i < 4; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"refresh_animation%lu", (unsigned long)i]];
            [refreshingImages addObject:image];
        }
        [header setImages:refreshingImages forState:MJRefreshStateRefreshing];
        header.lastUpdatedTimeLabel.hidden = YES;
        header.stateLabel.hidden = YES;
        _tableView.mj_header = header;

        //  设置下拉刷新
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        [footer setTitle:CustomStr(@"RefreshBackFooterIdleText") forState:MJRefreshStateIdle];
        [footer setTitle:CustomStr(@"RefreshBackFooterPullingText") forState:MJRefreshStatePulling];
        [footer setTitle:CustomStr(@"RefreshBackFooterRefreshingText") forState:MJRefreshStateRefreshing];
        [footer setTitle:CustomStr(@"RefreshBackFooterNoMoreDataText") forState:MJRefreshStateNoMoreData];
        _tableView.mj_footer = footer;
        
    }
    return _tableView;
}

- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
    }
    return _data;
}

@end