// // KBChildBuyLimitController.m // YouHuiProject // // Created by 小花 on 2018/7/6. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBChildBuyLimitController.h" #import "KBBuyLimitGoodCell.h" #import "DateManager.h" #import "KBBuyLimitHeader.h" #import "KBBuyLimitGoodModel.h" #import "KBGoodDetailViewController.h" @interface KBChildBuyLimitController () { NSInteger _page; } @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataArr; @property (nonatomic, strong) KBBuyLimitHeader *header; @property (nonatomic, strong) NSMutableArray *topArr; @end @implementation KBChildBuyLimitController - (void)dealloc { } - (void)viewDidLoad { [super viewDidLoad]; [self confitUI]; [self requestData]; } - (void)confitUI { _page = 1; self.view.backgroundColor = [UIColor whiteColor]; self.header = [[KBBuyLimitHeader alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160)]; __weak typeof(self) weakSelf = self; self.header.ClickBlock = ^(KBBuyLimitGoodModel *model) { KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *request = [[DetailRequestModel alloc] initWithBuyLimitModel:model]; detail.requestModel = request; KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:@"0" source:QuickBuyAction]; detail.eventModel = evevtModel; [weakSelf.navigationController pushViewController:detail animated:YES]; [MobClick event:BuyLimitGoodClick]; }; self.header.canBuy = self.canBuy; self.tableView.tableHeaderView = self.header; [self.view addSubview:self.tableView]; [MBProgressHUD showLoadingAddedToView:self.view]; } - (void)requestData { NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/taoQiangGouDataList",BaseURL]; NSTimeInterval start = [DateManager timeStrToInterval:self.start_time]; NSTimeInterval end = [DateManager timeStrToInterval:self.end_time]; NSDictionary *param = @{@"start_time":@(start), @"end_time":@(end), @"platform":@"2", @"page":@(_page), @"page_size":@(20) }; [KBHttp post:url params:param success:^(id json) { if ([self.tableView.mj_header isRefreshing]) { [self.topArr removeAllObjects]; [self.dataArr removeAllObjects]; } NSArray *topList = [NSArray yy_modelArrayWithClass:[KBBuyLimitGoodModel class] json:json[@"data"][@"top"]]; NSArray *goodList = [NSArray yy_modelArrayWithClass:[KBBuyLimitGoodModel class] json:json[@"data"][@"list"]]; if (goodList.count>0) { [self.topArr addObjectsFromArray:topList]; [self.dataArr addObjectsFromArray:goodList]; [self.tableView.mj_footer endRefreshing];; }else { if (_page==1) { [self setUpNoDataView]; } [self.tableView.mj_footer endRefreshingWithNoMoreData]; } [self setHeaderGoodInfo]; [self.tableView.mj_header endRefreshing]; [MBProgressHUD hideHUDForView:self.view]; [self.tableView reloadData]; } failure:^(NSError *error) { [self.tableView.mj_header endRefreshing]; [self.tableView.mj_footer endRefreshing]; [MBProgressHUD hideHUDForView:self.view]; }]; } - (void)setUpNoDataView { self.tableView.showNoDataView = YES; self.tableView.defaultNoDataText = @"暂无抢购的商品信息"; self.tableView.defaultNoDataImage = [UIImage imageNamed:@"noData"]; } - (void)setHeaderGoodInfo { if (self.topArr.count > 0) { KBBuyLimitGoodModel *model = self.topArr.firstObject; [self.header setModel:model]; self.header.canBuy = self.canBuy; } } #pragma mark -------- UITableView Delegate ----- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArr.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 124; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return .1; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { KBBuyLimitGoodModel *model = self.dataArr[indexPath.row]; KBBuyLimitGoodCell *cell = [KBBuyLimitGoodCell cellWithTableView:tableView]; cell.model = model; cell.canBuy = self.canBuy; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (!_canBuy) { [MBProgressHUD showMessage:@"抢购还未开始"]; return; } KBBuyLimitGoodModel *model = self.dataArr[indexPath.row]; if ([model.sale_rate floatValue]/100.0 > 1.f ) { [MBProgressHUD showMessage:@"已售空"]; return; } KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *request = [[DetailRequestModel alloc] initWithBuyLimitModel:model]; detail.requestModel = request; KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:@"0" source:QuickBuyAction]; detail.eventModel = evevtModel; [self.navigationController pushViewController:detail animated:YES]; [MobClick event:BuyLimitGoodClick]; } #pragma mark ------- layzer ------ - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-55) style:UITableViewStyleGrouped]; _tableView.estimatedSectionHeaderHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.sectionFooterHeight = 0; _tableView.sectionHeaderHeight = 0; _tableView.estimatedRowHeight = 0; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; _tableView.backgroundColor = [UIColor yhGrayColor]; _tableView.showsVerticalScrollIndicator = NO; _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; _tableView.separatorColor = [UIColor YHColorWithHex:0xEEEEEE]; MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{ _page = 1; [self requestData]; }]; header.lastUpdatedTimeLabel.hidden = YES; header.stateLabel.hidden = YES; //正在刷新的图片 NSArray *imgArr = @[[UIImage imageNamed:@"ant1"],[UIImage imageNamed:@"ant2"]]; [header setImages:imgArr duration:0.3 forState:MJRefreshStateRefreshing]; [header setImages:@[[UIImage imageNamed:@"ant1"]] duration:1 forState:MJRefreshStateWillRefresh]; [header setImages:@[[UIImage imageNamed:@"ant1"]] duration:1 forState:MJRefreshStatePulling]; _tableView.mj_header = header; _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ _page++; [self requestData]; }]; } return _tableView; } - (NSMutableArray *)dataArr { if (!_dataArr) { _dataArr = [NSMutableArray array]; } return _dataArr; } - (NSMutableArray *)topArr { if (!_topArr) { _topArr = [NSMutableArray array]; } return _topArr; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end