HomeProblemVC.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // HomeProblemVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/6/23.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "HomeProblemVC.h"
  9. #import "HomeSubModel.h"
  10. #import "HomeSubItemModel.h"
  11. #import "HomeProblemTopCell.h"
  12. #import "BookProblemModel.h"
  13. @interface HomeProblemVC ()<UITableViewDelegate,UITableViewDataSource>
  14. @property (nonatomic, strong) UITableView * tableView;
  15. @property (nonatomic, strong) NSMutableArray * dataSource;
  16. @property (nonatomic, assign) NSUInteger currentPage;
  17. @property (nonatomic, assign) NSUInteger Total;
  18. @property (nonatomic, strong) BookProblemModel * model;
  19. @end
  20. @implementation HomeProblemVC
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [self.view addSubview:self.tableView];
  24. self.tableView.delegate = self;
  25. self.tableView.dataSource = self;
  26. [self getData];
  27. [self setTableViewRefresh];
  28. }
  29. - (void)setHeight:(CGFloat)height
  30. {
  31. [self.tableView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, height - 45)];
  32. }
  33. - (UITableView *)tableView
  34. {
  35. if (!_tableView) {
  36. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  37. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  38. _tableView.showsVerticalScrollIndicator = NO;
  39. }
  40. return _tableView;
  41. }
  42. - (NSMutableArray *)dataSource
  43. {
  44. if (!_dataSource) {
  45. _dataSource = [NSMutableArray array];
  46. }
  47. return _dataSource;
  48. }
  49. - (void)setTableViewRefresh
  50. {
  51. WeakSelf(self)
  52. self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  53. [weakself headRefresh];
  54. }];
  55. self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
  56. [weakself footerRefresh];
  57. }];
  58. }
  59. - (void)headRefresh{
  60. self.currentPage = 1;
  61. self.Total = 0;
  62. [self.dataSource removeAllObjects];
  63. [self getListData];
  64. }
  65. - (void)footerRefresh{
  66. self.currentPage += 1;
  67. if (self.Total == self.dataSource.count) {
  68. self.currentPage --;
  69. [self.tableView.mj_footer resetNoMoreData];
  70. return;
  71. }
  72. [self getListData];
  73. }
  74. - (void)getData
  75. {
  76. WS(weakSelf);
  77. dispatch_semaphore_t sem = dispatch_semaphore_create(0);
  78. __block NSInteger httpFinishCount = 0;
  79. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  80. [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_RANDOM_DAY_Skill) parameters:@{@"StyleCss":weakSelf.style} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  81. NSLog(@"%@",responseObject);
  82. if ([responseObject isKindOfClass:[NSDictionary class]]) {
  83. weakSelf.model = [BookProblemModel modelWithDictionary:responseObject];
  84. }
  85. if (++httpFinishCount == 2) {
  86. dispatch_semaphore_signal(sem);
  87. }
  88. } failure:^(NSError * _Nonnull error) {
  89. if (++httpFinishCount == 2) {
  90. dispatch_semaphore_signal(sem);
  91. }
  92. }];
  93. [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_DAY_Skill) parameters:@{@"StyleCss":weakSelf.style,@"Page":@(weakSelf.currentPage),@"PerPage":@20} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  94. NSLog(@"++++%@",responseObject);
  95. if ([responseObject isKindOfClass:[NSDictionary class]]) {
  96. for (NSDictionary * dict in responseObject[@"Items"]) {
  97. HomeSubItemModel *model = [HomeSubItemModel modelWithDictionary:dict];
  98. [weakSelf.dataSource addObject:model];
  99. }
  100. weakSelf.Total = [responseObject[@"Total"] integerValue];
  101. if (weakSelf.dataSource.count == weakSelf.Total) {
  102. [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
  103. weakSelf.tableView.mj_footer.hidden = YES;
  104. }
  105. }
  106. [weakSelf.tableView.mj_header endRefreshing];
  107. [weakSelf.tableView.mj_footer endRefreshing];
  108. if (++httpFinishCount == 2) {
  109. dispatch_semaphore_signal(sem);
  110. }
  111. } failure:^(NSError * _Nonnull error) {
  112. if (++httpFinishCount == 2) {
  113. dispatch_semaphore_signal(sem);
  114. }
  115. }];
  116. dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
  117. dispatch_async(dispatch_get_main_queue(), ^{
  118. [weakSelf.tableView reloadData];
  119. });
  120. });
  121. }
  122. - (void)getListData
  123. {
  124. WS(weakSelf);
  125. [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_DAY_Skill) parameters:@{@"StyleCss":self.style,@"Page":@(self.currentPage),@"PerPage":@20} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  126. NSLog(@"++++%@",responseObject);
  127. if ([responseObject isKindOfClass:[NSDictionary class]]) {
  128. for (NSDictionary * dict in responseObject[@"Items"]) {
  129. HomeSubItemModel *model = [HomeSubItemModel modelWithDictionary:dict];
  130. [weakSelf.dataSource addObject:model];
  131. }
  132. weakSelf.Total = [responseObject[@"Total"] integerValue];
  133. if (weakSelf.dataSource.count == weakSelf.Total) {
  134. [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
  135. weakSelf.tableView.mj_footer.hidden = YES;
  136. }
  137. }
  138. [weakSelf.tableView.mj_header endRefreshing];
  139. [weakSelf.tableView.mj_footer endRefreshing];
  140. dispatch_async(dispatch_get_main_queue(), ^{
  141. [weakSelf.tableView reloadData];
  142. });
  143. } failure:^(NSError * _Nonnull error) {
  144. }];
  145. }
  146. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  147. {
  148. return 1;
  149. }
  150. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  151. {
  152. return self.dataSource.count;
  153. }
  154. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  155. {
  156. return UITableViewAutomaticDimension;
  157. }
  158. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  159. {
  160. WS(weakSelf);
  161. if (indexPath.row == 0) {
  162. HomeProblemTopCell * cell = [HomeProblemTopCell configCell0:tableView indexPath:indexPath];
  163. cell.height.constant = -2.f;
  164. [cell setDataModel:self.model];
  165. [cell.TopBtn setAction:^{
  166. [weakSelf pushVC:weakSelf.model.Id];
  167. }];
  168. return cell;
  169. }else{
  170. HomeSubItemModel * model = [self.dataSource objectAtIndex:indexPath.row];
  171. HomeProblemTopCell * cell = [HomeProblemTopCell configCell1:tableView indexPath:indexPath];
  172. [cell setDataListModel:model];
  173. return cell;
  174. }
  175. }
  176. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  177. {
  178. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  179. HomeSubItemModel * model = [self.dataSource objectAtIndex:indexPath.row];
  180. [self pushVC:model.Id];
  181. }
  182. - (void)pushVC:(NSInteger)Id
  183. {
  184. MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
  185. vc.Id = Id;
  186. vc.type = CollectModel_MediaAritle;
  187. [self.navigationController pushViewController:vc animated:YES];
  188. }
  189. @end