XDBarView.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #import "XDBarView.h"
  2. @interface XDBarView ()<UITableViewDataSource,UITableViewDelegate>{
  3. UILabel *label0, *label1, *label2, *label3, *label4, *label5;
  4. }
  5. /** footerview */
  6. @property (nonatomic,strong) UIView *footerview;
  7. /** 最大数 */
  8. @property (nonatomic,assign) CGFloat maxValue;
  9. @end
  10. @implementation XDBarView
  11. - (instancetype)initWithFrame:(CGRect)frame
  12. {
  13. if(self = [super initWithFrame:frame])
  14. {
  15. [self initViewDidLoad];
  16. }
  17. return self;
  18. }
  19. - (instancetype)init
  20. {
  21. if(self = [super init])
  22. {
  23. [self initViewDidLoad];
  24. }
  25. return self;
  26. }
  27. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  28. {
  29. if(self = [super initWithCoder:aDecoder])
  30. {
  31. [self initViewDidLoad];
  32. }
  33. return self;
  34. }
  35. - (UITableView *)tb{
  36. if (_tb == nil) {
  37. _tb = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  38. _tb.transform = CGAffineTransformMakeRotation(-M_PI / 2);//旋转-π/2
  39. _tb.frame = CGRectMake(0, 0,self.frame.size.width - 20,self.frame.size.height);
  40. _tb.delegate = self;
  41. _tb.dataSource = self;
  42. _tb.separatorStyle = NO;
  43. _tb.showsVerticalScrollIndicator = NO;
  44. _tb.bounces = NO;
  45. _tb.tableFooterView = self.footerview;
  46. }
  47. return _tb;
  48. }
  49. - (void) reloadtb {
  50. if (_tb) {
  51. if (_footerview) {
  52. UIView *view = [[_footerview subviews] firstObject];
  53. view.frame = CGRectMake(_barWith, -10,2,_cellHeight + 20);
  54. }
  55. [_tb reloadData];
  56. }
  57. }
  58. #pragma mark - 懒加载创建足视图footerview
  59. - (UIView *)footerview{
  60. if (_footerview == nil) {
  61. _footerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tb.frame.size.width,_cellHeight)];
  62. _footerview.backgroundColor = [UIColor clearColor];
  63. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(_barWith, -10,2,_cellHeight + 20)];
  64. line.backgroundColor = ColorWithHEAL;
  65. [_footerview addSubview:line];
  66. }
  67. return _footerview;
  68. }
  69. /** 初始化数据 设置背景view的属性*/
  70. - (void)initViewDidLoad
  71. {
  72. if(!_cellHeight){
  73. _cellHeight = 60;
  74. }
  75. if (!_barWith) {
  76. _barWith = 30;
  77. }
  78. self.backgroundColor = [UIColor whiteColor];
  79. }
  80. - (void)layoutSubviews
  81. {
  82. //添加子视图
  83. [self addSubview:self.tb];
  84. }
  85. - (void)newinitSelectedWithTableViewPath:(NSIndexPath *)indexPath{
  86. [self tableView:self.tb didSelectRowAtIndexPath:indexPath];
  87. }
  88. -(void)setModelArray:(NSArray *)modelArray{
  89. _modelArray = [NSArray arrayWithArray:modelArray];
  90. //arr为实际的模型取出的数组 只是为了获取最大值、、、、
  91. NSMutableArray * arr = [NSMutableArray array];
  92. for (int i= 0; i<_modelArray.count; i++) {
  93. if (i == 0) {
  94. if (_modelArray[i][@"value"]==nil) {
  95. return;
  96. }
  97. }
  98. [arr addObject:[NSString stringWithFormat:@"%@",_modelArray[i][@"value"]]];
  99. //NSLog(@"所有数字是:cptimes%@",_modelArray[i][@"cptimes"]);
  100. }
  101. _maxValue = [[arr valueForKeyPath:@"@max.intValue"] floatValue];
  102. if (_maxValue < 10.0) {
  103. _maxValue = 10.0;
  104. }else if (_maxValue < 50.0) {
  105. _maxValue = 50.0;
  106. }else if (_maxValue < 100.0) {
  107. _maxValue = 100.0;
  108. }else if (_maxValue < 250.0) {
  109. _maxValue = 250.0;
  110. }else if (_maxValue < 500.0) {
  111. _maxValue = 500.0;
  112. }else if (_maxValue < 1000.0) {
  113. _maxValue = 1000.0;
  114. }else if (_maxValue < 5000.0) {
  115. _maxValue = 5000.0;
  116. }else if (_maxValue < 10000.0) {
  117. _maxValue = 10000.0;
  118. }else if (_maxValue < 50000.0) {
  119. _maxValue = 50000.0;
  120. }else if (_maxValue < 100000.0) {
  121. _maxValue = 100000.0;
  122. }else {
  123. _maxValue = 1000000.0;
  124. }
  125. [_tb reloadData];
  126. }
  127. #pragma mark - 分组
  128. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  129. return 1;
  130. }
  131. //#pragma mark - 返回自定义区头
  132. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  133. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.height - 7, 50)];
  134. view.backgroundColor = [UIColor whiteColor];
  135. UILabel *unitLabel = [[UILabel alloc] init];
  136. unitLabel.transform = CGAffineTransformMakeRotation(M_PI / 2);
  137. unitLabel.frame = CGRectMake(self.frame.size.height - 18, 0.0, 18, 50);
  138. unitLabel.text = @"(人)";
  139. unitLabel.textColor = [UIColor darkGrayColor];
  140. unitLabel.font = Font_cu_PingFangSC_Medium_size(14);
  141. unitLabel.textAlignment = NSTextAlignmentCenter;
  142. [view addSubview:unitLabel];
  143. UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(_barWith, 44, self.frame.size.height - _barWith, 2)];
  144. lineView.backgroundColor = ColorWithHEAL;
  145. [view addSubview:lineView];
  146. CGFloat bd = (self.frame.size.height - _barWith - 50)/5.0;
  147. NSMutableArray *array = [NSMutableArray arrayWithCapacity:6];
  148. for (int i = 0; i < 6; i ++) {
  149. UIView *pointV = [[UIView alloc] initWithFrame:CGRectMake(_barWith + i*bd, 46, 2, 4)];
  150. pointV.backgroundColor = ColorWithHEAL;
  151. [view addSubview:pointV];
  152. UILabel *label = [[UILabel alloc] init];
  153. label.transform = CGAffineTransformMakeRotation(M_PI / 2);
  154. label.frame = CGRectMake(_barWith - 13.0 + i*bd, 0.0, 30, 40);
  155. label.textAlignment = NSTextAlignmentRight;
  156. label.font = Font_xi_PingFangSC_Light_size(14);
  157. label.textColor = [UIColor grayColor];
  158. label.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*i];
  159. [view addSubview:label];
  160. [array addObject:label];
  161. }
  162. // label0 = array[0];
  163. // label1 = array[1];
  164. // label2 = array[2];
  165. // label3 = array[3];
  166. // label4 = array[4];
  167. // label5 = array[5];
  168. //
  169. // label0.text = @"0";
  170. // label1.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*1];
  171. // label2.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*2];
  172. // label3.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*3];
  173. // label4.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*4];
  174. // label5.text = [NSString stringWithFormat:@"%.0f",_maxValue/5*5];
  175. return view;
  176. }
  177. //#pragma mark - 返回组头头高
  178. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  179. return 50;
  180. }
  181. #pragma mark - 每组几行
  182. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  183. return _modelArray.count;
  184. }
  185. #pragma mark - 每行行高
  186. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  187. return _cellHeight;
  188. }
  189. #pragma mark - tableViewData delegate
  190. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  191. XDBarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XDBarCell"];
  192. if (cell == nil) {
  193. cell = [[XDBarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"XDBarCell"];
  194. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  195. cell.cellLong = _tb.height;
  196. }
  197. [cell setCellHeight:_cellHeight barWidth:_barWith maxValue:_maxValue dataDictionary:_modelArray[indexPath.row]];
  198. return cell;
  199. }
  200. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  201. if (indexPath.row>=self.modelArray.count) {
  202. return ;
  203. }
  204. [self.tb scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
  205. self.SelectedWithTableViewPathRowBlock?self.SelectedWithTableViewPathRowBlock([NSString stringWithFormat:@"%@",_modelArray[indexPath.row][@"code"]],indexPath.row):nil;
  206. }
  207. - (void)selectedWithTableViewPathRowBlock:(SelectedWithTableViewPathRowBlock)block{
  208. self.SelectedWithTableViewPathRowBlock = block;
  209. }
  210. #pragma mark - 懒加载创建图示控件
  211. /*
  212. // Only override drawRect: if you perform custom drawing.
  213. // An empty implementation adversely affects performance during animation.
  214. - (void)drawRect:(CGRect)rect {
  215. // Drawing code 当调用needdisplay方法的时候 才自动调用这个方法
  216. }
  217. */
  218. @end