// // HomeRecommendVC.m // smartRhino // // Created by niuzhen on 2020/6/23. // Copyright © 2020 tederen. All rights reserved. // #import "HomeRecommendVC.h" #import "Item.h" #import "HomeTableViewCell.h" @interface HomeRecommendVC () @property (nonatomic, strong) UITableView * tableView; @property (nonatomic, strong) NSMutableArray * dataSource; @property (assign, nonatomic) NSInteger currentPage; @property (assign, nonatomic) NSInteger totalPage; @end @implementation HomeRecommendVC - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.tableView]; self.tableView.delegate = self; self.tableView.dataSource = self; [self setTableRefresh]; [self headRefresh]; // WS(weakSelf); // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // [weakSelf headRefresh]; // }); } - (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; } - (void)setTableRefresh { WS(weakSelf); self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [weakSelf headRefresh]; }]; self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ [weakSelf footerRefresh]; }]; } - (void)headRefresh{ self.currentPage = 1; self.totalPage = 1; [self.dataSource removeAllObjects]; [self getData]; } - (void)footerRefresh{ self.currentPage += 1; if (self.totalPage != 10) { self.currentPage --; [self.tableView.mj_footer endRefreshingWithNoMoreData]; self.tableView.mj_footer.hidden = YES; return ; } [self getData]; } - (NSMutableArray *)dataSource { if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } - (void)getData { WS(weakSelf); [[HttpManager sharedHttpManager] POSTUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Article_Notice_list_Post] parameters: @{@"page":@(self.currentPage),@"perPage":@10,@"GroupId":@(0)} responseStyle:JOSN success:^(id _Nonnull responseObject) { NSDictionary *dic = (NSDictionary *)responseObject; HomeArticleModel *model = [HomeArticleModel modelWithDictionary:dic]; [weakSelf.dataSource addObjectsFromArray:model.Items]; weakSelf.totalPage = model.Total; [weakSelf.tableView.mj_header endRefreshing]; [weakSelf.tableView.mj_footer endRefreshing]; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableView reloadData]; }); } failure:^(NSError * _Nonnull error) { [weakSelf.tableView.mj_header endRefreshing]; [weakSelf.tableView.mj_footer endRefreshing]; }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // Item *model = self.dataSource[indexPath.row]; // // if (model.ImageUrls.count == 0) { // model.cellID = 1; // }else if(model.ImageUrls.count == 1 || model.ImageUrls.count == 2){ // model.cellID = 2; // }else{ // model.cellID = 3; // } // switch (model.cellID) { // case 1: // { // return UITableViewAutomaticDimension; // } // break; // case 2: // return 112; // break; // case 3: // { // UILabel * label = [UILabel new]; // label.attributedText = [[NSAttributedString alloc] initWithString:model.Title attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size:18]}]; // label.numberOfLines = 2; // CGSize maxSize = CGSizeMake(SCREEN_WIDTH - 30.f, CGFLOAT_MAX); // CGSize newSize = [label sizeThatFits:maxSize]; // NSInteger height = 86.f / 345.f * (SCREEN_WIDTH - 30.f) + 52.5f + newSize.height; // return height; // } // break; // default: // return 70; // break; // } return UITableViewAutomaticDimension; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { Item *model = self.dataSource[indexPath.row]; if (model.ImageUrls.count == 0) { model.cellID = 1; }else if(model.ImageUrls.count == 1 || model.ImageUrls.count == 2){ model.cellID = 2; }else{ model.cellID = 3; } HomeTableViewCell *cell = [HomeTableViewCell cellWithTableView:tableView AndIndex:model.cellID]; if (cell != nil && self.dataSource.count > 0) { [cell.deleteButton setCurrentButtonHotSize:CGSizeZero]; [cell loadCurrentItemModel:model]; cell.deleteButton.hidden = YES; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; Item *model = self.dataSource[indexPath.row]; MyTDTopicDetailVC * vc = [MyTDTopicDetailVC initMyTDTopicDetailVC]; vc.type = CollectModel_Aritle; vc.Id = model.Id; vc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:vc animated:YES]; } @end