HomeSpeakVC.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // HomeSpeakVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/6/23.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "HomeSpeakVC.h"
  9. #import "HomeSubModel.h"
  10. #import "HomeSubItemModel.h"
  11. #import "HomeMusicCell.h"
  12. #import "BookListenVC.h"
  13. @interface HomeSpeakVC ()<UITableViewDelegate,UITableViewDataSource>
  14. @property (nonatomic, strong) UITableView * tableView;
  15. @property (nonatomic, strong) NSMutableArray * dataSource;
  16. @property (nonatomic, assign) NSInteger currentPage;
  17. @property (nonatomic, assign) NSInteger Total;
  18. @end
  19. @implementation HomeSpeakVC
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. [self.view addSubview:self.tableView];
  23. self.tableView.delegate = self;
  24. self.tableView.dataSource = self;
  25. self.currentPage = 1;
  26. self.Total = 0;
  27. WS(weakSelf);
  28. self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  29. [weakSelf footerRefresh];
  30. }];
  31. [self headRefresh];
  32. }
  33. - (void)setHeight:(CGFloat)height
  34. {
  35. [self.tableView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, height - 45)];
  36. }
  37. - (UITableView *)tableView
  38. {
  39. if (!_tableView) {
  40. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  41. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  42. _tableView.showsVerticalScrollIndicator = NO;
  43. }
  44. return _tableView;
  45. }
  46. - (NSMutableArray *)dataSource
  47. {
  48. if (!_dataSource) {
  49. _dataSource = [NSMutableArray array];
  50. }
  51. return _dataSource;
  52. }
  53. - (void)headRefresh{
  54. self.currentPage = 1;
  55. self.Total = 1;
  56. [self.dataSource removeAllObjects];
  57. [self getData];
  58. }
  59. - (void)footerRefresh{
  60. self.currentPage += 1;
  61. if (self.Total == self.dataSource.count) {
  62. self.currentPage --;
  63. [self.tableView.mj_footer endRefreshingWithNoMoreData];
  64. self.tableView.mj_footer.hidden = YES;
  65. return ;
  66. }
  67. [self getData];
  68. }
  69. - (void)getData
  70. {
  71. WS(weakSelf);
  72. [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_Chat_Hear_Speak) parameters:@{@"Page":@(self.currentPage),@"PerPage":@(20)} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  73. NSLog(@"%@",responseObject);
  74. [weakSelf.tableView.mj_footer endRefreshing];
  75. if ([responseObject isKindOfClass:[NSDictionary class]]) {
  76. HomeSubModel *model = [HomeSubModel modelWithDictionary:responseObject];
  77. for (NSDictionary * dict in model.Items) {
  78. HomeSubItemModel *smodel = [HomeSubItemModel modelWithDictionary:dict];
  79. [weakSelf.dataSource addObject:smodel];
  80. }
  81. weakSelf.Total = model.Total;
  82. if (weakSelf.Total == weakSelf.dataSource.count) {
  83. [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
  84. weakSelf.tableView.mj_footer.hidden = YES;
  85. }
  86. }
  87. dispatch_async(dispatch_get_main_queue(), ^{
  88. [weakSelf.tableView reloadData];
  89. });
  90. } failure:^(NSError * _Nonnull error) {
  91. }];
  92. }
  93. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  94. {
  95. return 1;
  96. }
  97. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  98. {
  99. return self.dataSource.count;
  100. }
  101. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  102. {
  103. return UITableViewAutomaticDimension;
  104. }
  105. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  106. {
  107. HomeSubItemModel *model = self.dataSource[indexPath.row];
  108. HomeMusicCell * cell = [HomeMusicCell configCell:tableView indexPath:indexPath];
  109. if (indexPath.row == 0) {
  110. cell.height.constant = -2.f;
  111. }
  112. [cell setDataWithModel:model];
  113. return cell;
  114. }
  115. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  118. HomeSubItemModel *model = self.dataSource[indexPath.row];
  119. BookListenVC * vc = [BookListenVC initBookListenVC];
  120. vc.Id = model.Id;
  121. vc.MediaType = model.MediaType;
  122. [self.navigationController pushViewController:vc animated:YES];
  123. }
  124. @end