// // KXLetterViewController.m // QBCS // // Created by kuxuan on 2017/6/12. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXLetterViewController.h" #import "KXLetterTableViewCell.h" #import "KXLetterModel.h" #import "KXMainDetailViewController.h" @interface KXLetterViewController () @property (nonatomic,strong)UITableView *tableView; @property (nonatomic,strong)NSMutableArray *dataSource; @property (nonatomic,assign)NSInteger page; @end @implementation KXLetterViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.name = @"消息"; self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeight-safeBottomHeight); [self createNavigation]; [self createTableView]; [self setHideBudeAction]; } - (void)setHideBudeAction{ if (self.hideblock) { self.hideblock(); } } -(void)createNavigation { [self addLeftBarButtonItemWithImageName:@"main_back" title:nil target:self selector:@selector(backAction)]; } -(void)backAction { [self.navigationController popViewControllerAnimated:YES]; } -(void)createTableView { self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeight-safeBottomHeight) style:UITableViewStylePlain]; self.tableView.backgroundColor=[UIColor KXColorWithRed:239 green:239 blue:244]; self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; [self.tableView registerClass:[KXLetterTableViewCell class] forCellReuseIdentifier:@"letter"]; //设置预估高度 self.tableView.estimatedRowHeight = 230; //真实高度 self.tableView.rowHeight = UITableViewAutomaticDimension; //下拉刷新 MJRefreshNormalHeader *header = [[MJRefreshNormalHeader alloc]init]; [header setRefreshingTarget:self refreshingAction:@selector(refreshWithHeader:)]; self.tableView.mj_header = header; [header beginRefreshing]; //上拉加载 MJRefreshAutoNormalFooter *footer=[[MJRefreshAutoNormalFooter alloc]init]; [footer setRefreshingTarget:self refreshingAction:@selector(refreshFooter:)]; self.tableView.mj_footer=footer; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; header.stateLabel.font = FONT_SYS(14); header.stateLabel.textColor=[UIColor detailTitleColor]; header.lastUpdatedTimeLabel.font=FONT_SYS(14); header.lastUpdatedTimeLabel.textColor=[UIColor detailTitleColor]; footer.stateLabel.textColor=[UIColor detailTitleColor]; } #pragma mark 下拉刷新和上拉加载 -(void)refreshWithHeader:(MJRefreshHeader *)header { self.page=1; [self requestDataWithPage:self.page]; } -(void)refreshFooter:(MJRefreshAutoFooter *)footer { self.page++; [self requestDataWithPage:self.page]; } -(void)requestDataWithPage:(NSInteger)page { NSString *urlString=[NSString stringWithFormat:@"%@/news/getNewsList",URL]; [KXHTTP post:urlString params:@{@"pageId":@(page)} success:^(id json) { if (self.page==1) { [self.dataSource removeAllObjects]; } NSArray *array=[NSArray yy_modelArrayWithClass:[KXLetterModel class] json:json[@"list"]]; [[NSUserDefaults standardUserDefaults]setObject:json[@"max_id"] forKey:MAX_ID]; [[NSUserDefaults standardUserDefaults]synchronize]; for (KXLetterModel *model in array) { [self.dataSource addObject:model]; } if (array.count < 10) { [_tableView.mj_header endRefreshing]; [_tableView.mj_footer endRefreshing]; [_tableView.mj_footer endRefreshingWithNoMoreData]; }else{ [_tableView.mj_header endRefreshing]; [_tableView.mj_footer endRefreshing]; } [_tableView reloadData]; } failure:^(NSError *error) { }]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { KXLetterModel *model=self.dataSource[indexPath.row]; KXLetterTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"letter"]; if (!cell) { cell=[[KXLetterTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"letter"]; } cell.selectionStyle=UITableViewCellSelectionStyleNone; cell.lettermodel=model; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [MobClick event:@"newsListClick"]; KXLetterModel *model=self.dataSource[indexPath.row]; KXMainDetailViewController *detail=[[KXMainDetailViewController alloc]init]; detail.detailID=model.product_id.stringValue; detail.name=model.title; detail.enterSource = @"news"; [self.navigationController pushViewController:detail animated:YES]; } -(NSMutableArray *)dataSource { if (!_dataSource) { _dataSource=[[NSMutableArray alloc]init]; } return _dataSource; } -(void)writeLetter { UIButton *writeBtn=[UIButton buttonWithType:UIButtonTypeCustom]; [writeBtn setTitle:@"title" forState:UIControlStateNormal]; } @end