HomeRecommendVC.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // HomeRecommendVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/6/23.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "HomeRecommendVC.h"
  9. #import "Item.h"
  10. #import "HomeTableViewCell.h"
  11. @interface HomeRecommendVC ()<UITableViewDelegate,UITableViewDataSource>
  12. @property (nonatomic, strong) UITableView * tableView;
  13. @property (nonatomic, strong) NSMutableArray * dataSource;
  14. @property (assign, nonatomic) NSInteger currentPage;
  15. @property (assign, nonatomic) NSInteger totalPage;
  16. @end
  17. @implementation HomeRecommendVC
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. [self.view addSubview:self.tableView];
  21. self.tableView.delegate = self;
  22. self.tableView.dataSource = self;
  23. [self setTableRefresh];
  24. [self headRefresh];
  25. }
  26. - (void)setHeight:(CGFloat)height
  27. {
  28. [self.tableView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, height - 45)];
  29. }
  30. - (UITableView *)tableView
  31. {
  32. if (!_tableView) {
  33. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  34. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  35. _tableView.showsVerticalScrollIndicator = NO;
  36. }
  37. return _tableView;
  38. }
  39. - (void)setTableRefresh
  40. {
  41. WS(weakSelf);
  42. self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  43. [weakSelf headRefresh];
  44. }];
  45. self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  46. [weakSelf footerRefresh];
  47. }];
  48. }
  49. - (void)headRefresh{
  50. self.currentPage = 1;
  51. self.totalPage = 1;
  52. [self.dataSource removeAllObjects];
  53. [self getData];
  54. }
  55. - (void)footerRefresh{
  56. self.currentPage += 1;
  57. if (self.totalPage != 10) {
  58. self.currentPage --;
  59. [self.tableView.mj_footer endRefreshingWithNoMoreData];
  60. self.tableView.mj_footer.hidden = YES;
  61. return ;
  62. }
  63. [self getData];
  64. }
  65. - (NSMutableArray *)dataSource
  66. {
  67. if (!_dataSource) {
  68. _dataSource = [NSMutableArray array];
  69. }
  70. return _dataSource;
  71. }
  72. - (void)getData
  73. {
  74. WS(weakSelf);
  75. [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Article_Notice_list_Post] parameters: @{@"page":@(self.currentPage),@"perPage":@10,@"GroupId":@(0)} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  76. NSDictionary *dic = (NSDictionary *)responseObject;
  77. HomeArticleModel *model = [HomeArticleModel modelWithDictionary:dic];
  78. [weakSelf.dataSource addObjectsFromArray:model.Items];
  79. weakSelf.totalPage = model.Total;
  80. [weakSelf.tableView.mj_header endRefreshing];
  81. [weakSelf.tableView.mj_footer endRefreshing];
  82. dispatch_async(dispatch_get_main_queue(), ^{
  83. [weakSelf.tableView reloadData];
  84. });
  85. } failure:^(NSError * _Nonnull error) {
  86. [weakSelf.tableView.mj_header endRefreshing];
  87. [weakSelf.tableView.mj_footer endRefreshing];
  88. }];
  89. }
  90. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  91. {
  92. return self.dataSource.count;
  93. }
  94. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  95. {
  96. // Item *model = self.dataSource[indexPath.row];
  97. //
  98. // if (model.ImageUrls.count == 0) {
  99. // model.cellID = 1;
  100. // }else if(model.ImageUrls.count == 1 || model.ImageUrls.count == 2){
  101. // model.cellID = 2;
  102. // }else{
  103. // model.cellID = 3;
  104. // }
  105. // switch (model.cellID) {
  106. // case 1:
  107. // {
  108. // return UITableViewAutomaticDimension;
  109. // }
  110. // break;
  111. // case 2:
  112. // return 112;
  113. // break;
  114. // case 3:
  115. // {
  116. // UILabel * label = [UILabel new];
  117. // label.attributedText = [[NSAttributedString alloc] initWithString:model.Title attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size:18]}];
  118. // label.numberOfLines = 2;
  119. // CGSize maxSize = CGSizeMake(SCREEN_WIDTH - 30.f, CGFLOAT_MAX);
  120. // CGSize newSize = [label sizeThatFits:maxSize];
  121. // NSInteger height = 86.f / 345.f * (SCREEN_WIDTH - 30.f) + 52.5f + newSize.height;
  122. // return height;
  123. // }
  124. // break;
  125. // default:
  126. // return 70;
  127. // break;
  128. // }
  129. return UITableViewAutomaticDimension;
  130. }
  131. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  132. {
  133. Item *model = self.dataSource[indexPath.row];
  134. if (model.ImageUrls.count == 0) {
  135. model.cellID = 1;
  136. }else if(model.ImageUrls.count == 1 || model.ImageUrls.count == 2){
  137. model.cellID = 2;
  138. }else{
  139. model.cellID = 3;
  140. }
  141. HomeTableViewCell *cell = [HomeTableViewCell cellWithTableView:tableView AndIndex:model.cellID];
  142. if (cell != nil && self.dataSource.count > 0) {
  143. [cell.deleteButton setCurrentButtonHotSize:CGSizeZero];
  144. [cell loadCurrentItemModel:model];
  145. cell.deleteButton.hidden = YES;
  146. }
  147. return cell;
  148. }
  149. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  150. {
  151. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  152. Item *model = self.dataSource[indexPath.row];
  153. MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
  154. vc.type = CollectModel_Aritle;
  155. vc.Id = model.Id;
  156. vc.hidesBottomBarWhenPushed = YES;
  157. [self.navigationController pushViewController:vc animated:YES];
  158. }
  159. @end