#import "XDBarView.h" @interface XDBarView (){ UILabel *label0, *label1, *label2, *label3, *label4, *label5; } /** footerview */ @property (nonatomic,strong) UIView *footerview; /** 最大数 */ @property (nonatomic,assign) CGFloat maxValue; @end @implementation XDBarView - (instancetype)initWithFrame:(CGRect)frame { if(self = [super initWithFrame:frame]) { [self initViewDidLoad]; } return self; } - (instancetype)init { if(self = [super init]) { [self initViewDidLoad]; } return self; } - (instancetype)initWithCoder:(NSCoder *)aDecoder { if(self = [super initWithCoder:aDecoder]) { [self initViewDidLoad]; } return self; } - (UITableView *)tb{ if (_tb == nil) { _tb = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tb.transform = CGAffineTransformMakeRotation(-M_PI / 2);//旋转-π/2 _tb.frame = CGRectMake(0, 0,self.frame.size.width - 20,self.frame.size.height); _tb.delegate = self; _tb.dataSource = self; _tb.separatorStyle = NO; _tb.showsVerticalScrollIndicator = NO; _tb.bounces = NO; _tb.tableFooterView = self.footerview; } return _tb; } - (void) reloadtb { if (_tb) { if (_footerview) { UIView *view = [[_footerview subviews] firstObject]; view.frame = CGRectMake(_barWith, -10,2,_cellHeight + 20); } [_tb reloadData]; } } #pragma mark - 懒加载创建足视图footerview - (UIView *)footerview{ if (_footerview == nil) { _footerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tb.frame.size.width,_cellHeight)]; _footerview.backgroundColor = [UIColor clearColor]; UIView *line = [[UIView alloc] initWithFrame:CGRectMake(_barWith, -10,2,_cellHeight + 20)]; line.backgroundColor = ColorWithHEAL; [_footerview addSubview:line]; } return _footerview; } /** 初始化数据 设置背景view的属性*/ - (void)initViewDidLoad { if(!_cellHeight){ _cellHeight = 60; } if (!_barWith) { _barWith = 30; } self.backgroundColor = [UIColor whiteColor]; } - (void)layoutSubviews { //添加子视图 [self addSubview:self.tb]; } - (void)newinitSelectedWithTableViewPath:(NSIndexPath *)indexPath{ [self tableView:self.tb didSelectRowAtIndexPath:indexPath]; } -(void)setModelArray:(NSArray *)modelArray{ _modelArray = [NSArray arrayWithArray:modelArray]; //arr为实际的模型取出的数组 只是为了获取最大值、、、、 NSMutableArray * arr = [NSMutableArray array]; for (int i= 0; i<_modelArray.count; i++) { if (i == 0) { if (_modelArray[i][@"value"]==nil) { return; } } [arr addObject:[NSString stringWithFormat:@"%@",_modelArray[i][@"value"]]]; //NSLog(@"所有数字是:cptimes%@",_modelArray[i][@"cptimes"]); } _maxValue = [[arr valueForKeyPath:@"@max.intValue"] floatValue]; if (_maxValue < 10.0) { _maxValue = 10.0; }else if (_maxValue < 50.0) { _maxValue = 50.0; }else if (_maxValue < 100.0) { _maxValue = 100.0; }else if (_maxValue < 250.0) { _maxValue = 250.0; }else if (_maxValue < 500.0) { _maxValue = 500.0; }else if (_maxValue < 1000.0) { _maxValue = 1000.0; }else if (_maxValue < 5000.0) { _maxValue = 5000.0; }else if (_maxValue < 10000.0) { _maxValue = 10000.0; }else if (_maxValue < 50000.0) { _maxValue = 50000.0; }else if (_maxValue < 100000.0) { _maxValue = 100000.0; }else { _maxValue = 1000000.0; } [_tb reloadData]; } #pragma mark - 分组 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } //#pragma mark - 返回自定义区头 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.height - 7, 50)]; view.backgroundColor = [UIColor whiteColor]; UILabel *unitLabel = [[UILabel alloc] init]; unitLabel.transform = CGAffineTransformMakeRotation(M_PI / 2); unitLabel.frame = CGRectMake(self.frame.size.height - 18, 0.0, 18, 50); unitLabel.text = @"(人)"; unitLabel.textColor = [UIColor darkGrayColor]; unitLabel.font = Font_cu_PingFangSC_Medium_size(14); unitLabel.textAlignment = NSTextAlignmentCenter; [view addSubview:unitLabel]; UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(_barWith, 44, self.frame.size.height - _barWith, 2)]; lineView.backgroundColor = ColorWithHEAL; [view addSubview:lineView]; CGFloat bd = (self.frame.size.height - _barWith - 50)/5.0; NSMutableArray *array = [NSMutableArray arrayWithCapacity:6]; for (int i = 0; i < 6; i ++) { UIView *pointV = [[UIView alloc] initWithFrame:CGRectMake(_barWith + i*bd, 46, 2, 4)]; pointV.backgroundColor = ColorWithHEAL; [view addSubview:pointV]; UILabel *label = [[UILabel alloc] init]; label.transform = CGAffineTransformMakeRotation(M_PI / 2); label.frame = CGRectMake(_barWith - 13.0 + i*bd, 0.0, 30, 40); label.textAlignment = NSTextAlignmentRight; label.font = Font_xi_PingFangSC_Light_size(14); label.textColor = [UIColor grayColor]; label.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*i]; [view addSubview:label]; [array addObject:label]; } // label0 = array[0]; // label1 = array[1]; // label2 = array[2]; // label3 = array[3]; // label4 = array[4]; // label5 = array[5]; // // label0.text = @"0"; // label1.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*1]; // label2.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*2]; // label3.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*3]; // label4.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*4]; // label5.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*5]; return view; } //#pragma mark - 返回组头头高 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 50; } #pragma mark - 每组几行 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _modelArray.count; } #pragma mark - 每行行高 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return _cellHeight; } #pragma mark - tableViewData delegate - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ XDBarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XDBarCell"]; if (cell == nil) { cell = [[XDBarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"XDBarCell"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.cellLong = _tb.height; } [cell setCellHeight:_cellHeight barWidth:_barWith maxValue:_maxValue dataDictionary:_modelArray[indexPath.row]]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row>=self.modelArray.count) { return ; } [self.tb scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; self.SelectedWithTableViewPathRowBlock?self.SelectedWithTableViewPathRowBlock([NSString stringWithFormat:@"%@",_modelArray[indexPath.row][@"code"]],indexPath.row):nil; } - (void)selectedWithTableViewPathRowBlock:(SelectedWithTableViewPathRowBlock)block{ self.SelectedWithTableViewPathRowBlock = block; } #pragma mark - 懒加载创建图示控件 /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code 当调用needdisplay方法的时候 才自动调用这个方法 } */ @end