説明なし

KXLetterViewController.m 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // KXLetterViewController.m
  3. // QBCS
  4. //
  5. // Created by kuxuan on 2017/6/12.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXLetterViewController.h"
  9. #import "KXLetterTableViewCell.h"
  10. #import "KXLetterModel.h"
  11. #import "KXMainDetailViewController.h"
  12. @interface KXLetterViewController ()<UITableViewDelegate,UITableViewDataSource>
  13. @property (nonatomic,strong)UITableView *tableView;
  14. @property (nonatomic,strong)NSMutableArray *dataSource;
  15. @property (nonatomic,assign)NSInteger page;
  16. @end
  17. @implementation KXLetterViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view.
  21. self.name = @"消息";
  22. self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeight-safeBottomHeight);
  23. [self createNavigation];
  24. [self createTableView];
  25. [self setHideBudeAction];
  26. }
  27. - (void)setHideBudeAction{
  28. if (self.hideblock) {
  29. self.hideblock();
  30. }
  31. }
  32. -(void)createNavigation
  33. {
  34. [self addLeftBarButtonItemWithImageName:@"main_back" title:nil target:self selector:@selector(backAction)];
  35. }
  36. -(void)backAction
  37. {
  38. [self.navigationController popViewControllerAnimated:YES];
  39. }
  40. -(void)createTableView
  41. {
  42. self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeight-safeBottomHeight) style:UITableViewStylePlain];
  43. self.tableView.backgroundColor=[UIColor KXColorWithRed:239 green:239 blue:244];
  44. self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
  45. [self.tableView registerClass:[KXLetterTableViewCell class] forCellReuseIdentifier:@"letter"];
  46. //设置预估高度
  47. self.tableView.estimatedRowHeight = 230;
  48. //真实高度
  49. self.tableView.rowHeight = UITableViewAutomaticDimension;
  50. //下拉刷新
  51. MJRefreshNormalHeader *header =
  52. [[MJRefreshNormalHeader alloc]init];
  53. [header setRefreshingTarget:self refreshingAction:@selector(refreshWithHeader:)];
  54. self.tableView.mj_header = header;
  55. [header beginRefreshing];
  56. //上拉加载
  57. MJRefreshAutoNormalFooter *footer=[[MJRefreshAutoNormalFooter alloc]init];
  58. [footer setRefreshingTarget:self refreshingAction:@selector(refreshFooter:)];
  59. self.tableView.mj_footer=footer;
  60. self.tableView.delegate = self;
  61. self.tableView.dataSource = self;
  62. [self.view addSubview:self.tableView];
  63. header.stateLabel.font = FONT_SYS(14);
  64. header.stateLabel.textColor=[UIColor detailTitleColor];
  65. header.lastUpdatedTimeLabel.font=FONT_SYS(14);
  66. header.lastUpdatedTimeLabel.textColor=[UIColor detailTitleColor];
  67. footer.stateLabel.textColor=[UIColor detailTitleColor];
  68. }
  69. #pragma mark 下拉刷新和上拉加载
  70. -(void)refreshWithHeader:(MJRefreshHeader *)header
  71. {
  72. self.page=1;
  73. [self requestDataWithPage:self.page];
  74. }
  75. -(void)refreshFooter:(MJRefreshAutoFooter *)footer
  76. {
  77. self.page++;
  78. [self requestDataWithPage:self.page];
  79. }
  80. -(void)requestDataWithPage:(NSInteger)page
  81. {
  82. NSString *urlString=[NSString stringWithFormat:@"%@/news/getNewsList",URL];
  83. [KXHTTP post:urlString params:@{@"pageId":@(page)} success:^(id json) {
  84. if (self.page==1) {
  85. [self.dataSource removeAllObjects];
  86. }
  87. NSArray *array=[NSArray yy_modelArrayWithClass:[KXLetterModel class] json:json[@"list"]];
  88. [[NSUserDefaults standardUserDefaults]setObject:json[@"max_id"] forKey:MAX_ID];
  89. [[NSUserDefaults standardUserDefaults]synchronize];
  90. for (KXLetterModel *model in array) {
  91. [self.dataSource addObject:model];
  92. }
  93. if (array.count < 10) {
  94. [_tableView.mj_header endRefreshing];
  95. [_tableView.mj_footer endRefreshing];
  96. [_tableView.mj_footer endRefreshingWithNoMoreData];
  97. }else{
  98. [_tableView.mj_header endRefreshing];
  99. [_tableView.mj_footer endRefreshing];
  100. }
  101. [_tableView reloadData];
  102. } failure:^(NSError *error) {
  103. }];
  104. }
  105. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  106. {
  107. return self.dataSource.count;
  108. }
  109. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  110. {
  111. KXLetterModel *model=self.dataSource[indexPath.row];
  112. KXLetterTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"letter"];
  113. if (!cell) {
  114. cell=[[KXLetterTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"letter"];
  115. }
  116. cell.selectionStyle=UITableViewCellSelectionStyleNone;
  117. cell.lettermodel=model;
  118. return cell;
  119. }
  120. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. [MobClick event:@"newsListClick"];
  123. KXLetterModel *model=self.dataSource[indexPath.row];
  124. KXMainDetailViewController *detail=[[KXMainDetailViewController alloc]init];
  125. detail.detailID=model.product_id.stringValue;
  126. detail.name=model.title;
  127. detail.enterSource = @"news";
  128. [self.navigationController pushViewController:detail animated:YES];
  129. }
  130. -(NSMutableArray *)dataSource
  131. {
  132. if (!_dataSource) {
  133. _dataSource=[[NSMutableArray alloc]init];
  134. }
  135. return _dataSource;
  136. }
  137. -(void)writeLetter
  138. {
  139. UIButton *writeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  140. [writeBtn setTitle:@"title" forState:UIControlStateNormal];
  141. }
  142. @end