HomeRecommendVC.m 5.5 KB

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