// // RankingListViewController.m // tongxin // // Created by WeiChaoZheng on 2018/8/24. // Copyright © 2018年 xTT. All rights reserved. // #import "RankingListViewController.h" #import "RankingListTableViewCell.h" #import "RankingMyListTableViewCell.h" #import "SwitchDeviceView.h" #import "Step.h" #define tableViewCellHeight 60 @interface RankingListViewController () /** 导航栏自定义按钮 */ @property (strong , nonatomic) UIButton *rightBtn; /** 自定义按钮的边角 */ @property (strong , nonatomic) UIButton *rightBtnCorner; /** 切换榜单的View */ @property (nonatomic, strong) SwitchDeviceView * switchView; /** 自己的信息 */ @property (nonatomic, strong) Step * infoStep; @end @implementation RankingListViewController @synthesize myDataSource = _myDataSource; - (Step *)infoStep{ if (!_infoStep) { _infoStep = [[Step alloc] init]; _infoStep.imei = cUser.cDevice.imei; _infoStep.name = cUser.cDevice.name; _infoStep.phone = cUser.cDevice.phone; _infoStep.image = cUser.cDevice.image; _infoStep.avator = cUser.cDevice.avator; _infoStep.sex = cUser.cDevice.sex; } return _infoStep; } -(RankingListHeadView *)listHeadView{ if(!_listHeadView){ _listHeadView = [RankingListHeadView viewFirstXib]; } return _listHeadView; } - (void)viewDidLoad { [super viewDidLoad]; self.title = @"排行榜"; self.myDataSource = [NSMutableArray array]; [self.myTableView registerNib:[UINib nibWithNibName:@"RankingListHeadCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"RankingListHeadCellID"]; [self.myTableView registerNib:[UINib nibWithNibName:@"RankingListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"RankingListTableViewCellID"]; [self.myTableView registerNib:[UINib nibWithNibName:@"RankingMyListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"RankingMyListTableViewCellID"]; [self initView]; WEAKSELF self.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [weakSelf switchListModelWithData:weakSelf.rightBtn.titleLabel.text]; }]; self.scrollView.showsVerticalScrollIndicator = NO; self.scrollView.showsHorizontalScrollIndicator = NO; self.myTableView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9]; self.myTableView.bounces = NO; //去线 self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.myTableView.layer.cornerRadius = 5; self.myTableView.layer.masksToBounds = YES; self.myTableView.showsVerticalScrollIndicator = NO; self.myTableView.showsHorizontalScrollIndicator = NO; self.myTableView.userInteractionEnabled = NO; self.tableViewHeight.constant = tableViewCellHeight; // 默认是一个cell高度而已 //添加没有设备时 显示的 View [self.scrollView addSubview:self.listHeadView]; [self.listHeadView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_offset(0); make.left.right.mas_offset(0); make.height.mas_offset(200); }]; } -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ [self.headUpdateLabel setHidden:YES]; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if(scrollView.contentOffset.y == 0){ [self.headUpdateLabel setHidden:NO]; } } -(void)initView{ _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _rightBtn.frame = CGRectMake(0, 0, 70, 21); _rightBtnCorner = [UIButton buttonWithType:UIButtonTypeCustom]; _rightBtnCorner.frame = CGRectMake(0, 0, 12, 21); [_rightBtnCorner setImage:[UIImage imageNamed:@"ic_arrow_drop_down_white"] forState:0]; [_rightBtnCorner addTarget:self action:@selector(switchListModel:) forControlEvents:UIControlEventTouchUpInside]; [_rightBtnCorner setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)]; // [_rightBtn setImage:image forState:0]; // [_rightBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 65, 0, -20)]; [_rightBtn setTitleColor:[UIColor whiteColor] forState:0]; _rightBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [_rightBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight]; [_rightBtn setTitle:@"日榜" forState:0]; [_rightBtn addTarget:self action:@selector(switchListModel:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn]; UIBarButtonItem *imageBarBtn = [[UIBarButtonItem alloc] initWithCustomView:_rightBtnCorner]; UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = -15; self.navigationItem.rightBarButtonItems = @[negativeSpacer,imageBarBtn,rightBarBtn]; self.headUpdateLabel.textColor = [UIColor whiteColor]; //185 191 201 self.headUpdateLabel.backgroundColor = RGBA(185, 191, 201, 1); //默认隐藏 [self.headUpdateLabel setHidden:YES]; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self switchListModelWithData:@"日榜"]; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; if(self.rightBtn.isSelected){ self.rightBtn.selected = NO; [self.switchView setHidden:YES]; } } -(void)switchListModel:(UIButton *)sender{ self.rightBtn.selected = !self.rightBtn.selected; if(self.rightBtn.isSelected){ [_rightBtnCorner setImage:[UIImage imageNamed:@"ic_arrow_drop_up_white"] forState:0]; }else{ [_rightBtnCorner setImage:[UIImage imageNamed:@"ic_arrow_drop_down_white"] forState:0]; } CGPoint p = CGPointMake(ScreenWidth - 30, getRectNavAndStatusHight - 10); [YBPopupMenu showAtPoint:p titles:@[@"日榜",@"周榜",@"月榜"] icons:nil menuWidth:80*(ScreenWidth/320) otherSettings:^(YBPopupMenu *popupMenu) { popupMenu.textColor = [UIColor blackColor]; popupMenu.fontSize = 15; popupMenu.cornerRadius = 4.0f; popupMenu.dismissOnSelected = NO; popupMenu.delegate = self; popupMenu.offset = 5; popupMenu.type = YBPopupMenuTypeDark; }]; } #pragma mark - YBPopupMenuDelegate 代理方法 - (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu { [ybPopupMenu dismiss]; NSArray * arr = @[@"日榜",@"周榜",@"月榜"]; [self switchListModelWithData:arr[index]]; [self.rightBtn setTitle:arr[index] forState:0]; self.rightBtn.selected = !self.rightBtn.selected; if(self.rightBtn.isSelected){ [_rightBtnCorner setImage:[UIImage imageNamed:@"ic_arrow_drop_up_white"] forState:0]; }else{ [_rightBtnCorner setImage:[UIImage imageNamed:@"ic_arrow_drop_down_white"] forState:0]; } } -(void)switchListModelWithData:(NSString *)title{ NSMutableDictionary *params = [NSMutableDictionary dictionary]; if([title isEqualToString:@"日榜"]){ //日榜 NSString *today = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" timeInterval:[NSDate date].timeIntervalSince1970]; [params setValue:today forKey:@"day"]; }else if([title isEqualToString:@"周榜"]){ //周榜 [params setValue:@(1) forKey:@"week"]; }else{ //月榜 [params setValue:@(1) forKey:@"month"]; } [self.myDataSource removeAllObjects]; WEAKSELF [Step getRankingListParams:params Success:^(NSDictionary *dic) { //更新时间 weakSelf.headUpdateLabel.text = [NSString stringWithFormat:@"%@ 更新",[myHelper getDateFormatWithStr:@"HH:mm" timeInterval:[dic[@"update_time"] doubleValue]]]; [self.headUpdateLabel setHidden:NO]; weakSelf.infoStep.num = dic[@"my_num"]; weakSelf.infoStep.my_ranking = dic[@"my_ranking"]; NSArray *listArr = dic[@"ranking_list"]; // 除了自己的排名. NSMutableArray *firstArr = [NSMutableArray array]; NSMutableArray *secondArr = [NSMutableArray array]; for (int i=0; i 0){ [self.listHeadView setDataWithListHead:firstArr]; }else{ [self.listHeadView setDataWithListHead:nil]; } [secondArr insertObject:weakSelf.infoStep atIndex:0]; weakSelf.myDataSource = secondArr; CGFloat tableViewHeight = tableViewCellHeight*(secondArr.count); weakSelf.tableViewHeight.constant = tableViewHeight; weakSelf.myTableView.contentSize = CGSizeMake(weakSelf.myTableView.contentSize.width, tableViewHeight); [weakSelf.myTableView reloadData]; [weakSelf.scrollView.mj_header endRefreshing]; } failure:^(NSError *error) { [weakSelf.scrollView.mj_header endRefreshing]; }]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.myDataSource count]; } //行高 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return tableViewCellHeight; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewCellEditingStyleNone; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row == 0){ //我 RankingMyListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankingMyListTableViewCellID" forIndexPath:indexPath]; [cell setMyDataWith:self.infoStep]; if([self.myDataSource count] == 1){ //表明除了 自己没有其他 [cell setLineViewHidden:YES]; }else{ [cell setLineViewHidden:NO]; } return cell; }else{ //排行 RankingListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankingListTableViewCellID" forIndexPath:indexPath]; [cell setDataWith:self.myDataSource[indexPath.row]]; if(indexPath.row == [self.myDataSource count] - 1){ [cell setLineViewHidden:YES]; }else{ [cell setLineViewHidden:NO]; } return cell; } return nil; } - (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