123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- //
- // HomeProblemVC.m
- // smartRhino
- //
- // Created by niuzhen on 2020/6/23.
- // Copyright © 2020 tederen. All rights reserved.
- //
- #import "HomeProblemVC.h"
- #import "HomeSubModel.h"
- #import "HomeSubItemModel.h"
- #import "HomeProblemTopCell.h"
- #import "BookProblemModel.h"
- @interface HomeProblemVC ()<UITableViewDelegate,UITableViewDataSource>
- @property (nonatomic, strong) UITableView * tableView;
- @property (nonatomic, strong) NSMutableArray * dataSource;
- @property (nonatomic, assign) NSUInteger currentPage;
- @property (nonatomic, assign) NSUInteger Total;
- @property (nonatomic, strong) BookProblemModel * model;
- @end
- @implementation HomeProblemVC
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self.view addSubview:self.tableView];
- self.tableView.delegate = self;
- self.tableView.dataSource = self;
- [self getData];
- [self setTableViewRefresh];
- }
- - (void)setHeight:(CGFloat)height
- {
- [self.tableView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, height - 45)];
- }
- - (UITableView *)tableView
- {
- if (!_tableView) {
- _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
- _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- _tableView.showsVerticalScrollIndicator = NO;
- }
- return _tableView;
- }
- - (NSMutableArray *)dataSource
- {
- if (!_dataSource) {
- _dataSource = [NSMutableArray array];
- }
- return _dataSource;
- }
- - (void)setTableViewRefresh
- {
- WeakSelf(self)
- self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
- [weakself headRefresh];
- }];
- self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
- [weakself footerRefresh];
- }];
- }
- - (void)headRefresh{
- self.currentPage = 1;
- self.Total = 0;
- [self.dataSource removeAllObjects];
- [self getListData];
- }
- - (void)footerRefresh{
- self.currentPage += 1;
- if (self.Total == self.dataSource.count) {
- self.currentPage --;
- [self.tableView.mj_footer resetNoMoreData];
- return;
- }
- [self getListData];
- }
- - (void)getData
- {
- WS(weakSelf);
- dispatch_semaphore_t sem = dispatch_semaphore_create(0);
- __block NSInteger httpFinishCount = 0;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_RANDOM_DAY_Skill) parameters:@{@"StyleCss":weakSelf.style} responseStyle:JOSN success:^(id _Nonnull responseObject) {
- NSLog(@"%@",responseObject);
- if ([responseObject isKindOfClass:[NSDictionary class]]) {
- weakSelf.model = [BookProblemModel modelWithDictionary:responseObject];
- }
- if (++httpFinishCount == 2) {
- dispatch_semaphore_signal(sem);
- }
- } failure:^(NSError * _Nonnull error) {
- if (++httpFinishCount == 2) {
- dispatch_semaphore_signal(sem);
- }
- }];
- [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_DAY_Skill) parameters:@{@"StyleCss":weakSelf.style,@"Page":@(weakSelf.currentPage),@"PerPage":@20} responseStyle:JOSN success:^(id _Nonnull responseObject) {
- NSLog(@"++++%@",responseObject);
- if ([responseObject isKindOfClass:[NSDictionary class]]) {
- for (NSDictionary * dict in responseObject[@"Items"]) {
- HomeSubItemModel *model = [HomeSubItemModel modelWithDictionary:dict];
- [weakSelf.dataSource addObject:model];
- }
- weakSelf.Total = [responseObject[@"Total"] integerValue];
- if (weakSelf.dataSource.count == weakSelf.Total) {
- [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
- weakSelf.tableView.mj_footer.hidden = YES;
- }
- }
- [weakSelf.tableView.mj_header endRefreshing];
- [weakSelf.tableView.mj_footer endRefreshing];
- if (++httpFinishCount == 2) {
- dispatch_semaphore_signal(sem);
- }
- } failure:^(NSError * _Nonnull error) {
- if (++httpFinishCount == 2) {
- dispatch_semaphore_signal(sem);
- }
- }];
- dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
- dispatch_async(dispatch_get_main_queue(), ^{
- [weakSelf.tableView reloadData];
- });
- });
- }
- - (void)getListData
- {
- WS(weakSelf);
- [[HttpManager sharedHttpManager] POSTUrl:Host(API_APP_DAY_Skill) parameters:@{@"StyleCss":self.style,@"Page":@(self.currentPage),@"PerPage":@20} responseStyle:JOSN success:^(id _Nonnull responseObject) {
- NSLog(@"++++%@",responseObject);
- if ([responseObject isKindOfClass:[NSDictionary class]]) {
- for (NSDictionary * dict in responseObject[@"Items"]) {
- HomeSubItemModel *model = [HomeSubItemModel modelWithDictionary:dict];
- [weakSelf.dataSource addObject:model];
- }
- weakSelf.Total = [responseObject[@"Total"] integerValue];
- if (weakSelf.dataSource.count == weakSelf.Total) {
- [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
- weakSelf.tableView.mj_footer.hidden = YES;
- }
- }
- [weakSelf.tableView.mj_header endRefreshing];
- [weakSelf.tableView.mj_footer endRefreshing];
- dispatch_async(dispatch_get_main_queue(), ^{
- [weakSelf.tableView reloadData];
- });
- } failure:^(NSError * _Nonnull error) {
- }];
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return self.dataSource.count;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewAutomaticDimension;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- WS(weakSelf);
- if (indexPath.row == 0) {
- HomeProblemTopCell * cell = [HomeProblemTopCell configCell0:tableView indexPath:indexPath];
- cell.height.constant = -2.f;
- [cell setDataModel:self.model];
- [cell.TopBtn setAction:^{
- [weakSelf pushVC:weakSelf.model.Id];
- }];
- return cell;
- }else{
- HomeSubItemModel * model = [self.dataSource objectAtIndex:indexPath.row];
- HomeProblemTopCell * cell = [HomeProblemTopCell configCell1:tableView indexPath:indexPath];
- [cell setDataListModel:model];
- return cell;
- }
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- HomeSubItemModel * model = [self.dataSource objectAtIndex:indexPath.row];
- [self pushVC:model.Id];
- }
- - (void)pushVC:(NSInteger)Id
- {
- MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC];
- vc.Id = Id;
- vc.type = CollectModel_MediaAritle;
- [self.navigationController pushViewController:vc animated:YES];
- }
- @end
|