TrainInfoTableViewCell.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // TrainInfoTableViewCell.m
  3. // jiaPei
  4. //
  5. // Created by apple on 2017/2/23.
  6. // Copyright © 2017年 JCZ. All rights reserved.
  7. //
  8. #import "TrainInfoTableViewCell.h"
  9. @implementation TrainInfoTableViewCell
  10. - (void)awakeFromNib {
  11. [super awakeFromNib];
  12. }
  13. + (TrainInfoTableViewCell*)cellForTabelView:(UITableView*)tableView
  14. {
  15. TrainInfoTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"trainInfoTableViewCell"];
  16. if (!cell) {
  17. cell = [[TrainInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"trainInfoTableViewCell"];
  18. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  19. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  20. cell.backgroundColor = backGroundColor;
  21. }
  22. return cell;
  23. }
  24. -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  25. {
  26. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  27. if (self) {
  28. self.backgroundColor = backGroundColor;
  29. CGFloat x,y,w,h;
  30. x = 20;
  31. y = 10;
  32. w = 50;
  33. h = 25;
  34. UILabel *label = [[UILabel alloc] setxywh];
  35. [label setText:@"" Font:FontTitle TextColor:kTitleColor];
  36. [self addSubview:label];
  37. titleLabel = label;
  38. x += w + 5;
  39. y += 5;
  40. w = kSize.width - 2*x;
  41. h = 15;
  42. label = [[UILabel alloc] setxywh];
  43. label.backgroundColor = RQlineColor;
  44. label.layer.masksToBounds = YES;
  45. label.layer.cornerRadius = 7;
  46. [self addSubview:label];
  47. allLabel = label;
  48. label = [[UILabel alloc] setxywh];
  49. label.backgroundColor = RQ_MAIN_COLOR;
  50. label.layer.masksToBounds = YES;
  51. label.layer.cornerRadius = 7;
  52. [self addSubview:label];
  53. label.hidden = YES;
  54. nowLabel = label;
  55. x += w;
  56. y -= 5;
  57. w = 60;
  58. h = 25;
  59. label = [[UILabel alloc] setxywh];
  60. [label setText:@"" Font:FontTitle TextColor:kTitleColor];
  61. [self addSubview:label];
  62. progressLabel = label;
  63. x = 65;
  64. y += h + 10;
  65. w = kSize.width - x;
  66. h = 25;
  67. label = [[UILabel alloc] setxywh];
  68. [label setText:@"" Font:FontTitle TextColor:kTitleColor];
  69. [self addSubview:label];
  70. contentLabel = label;
  71. }
  72. return self;
  73. }
  74. - (void)setDataArray:(NSMutableArray *)dataArray {
  75. _dataArray = dataArray;
  76. //防止除0
  77. if (_allNeedTime == 0) {
  78. _allNeedTime = 240;
  79. }
  80. NSInteger allHour = _allNeedTime/(_RATIONKS? : 60);
  81. NSInteger allMin = _allNeedTime%(_RATIONKS? : 60);
  82. NSString *allTimeString = [NSString stringWithFormat:@"%d%@%d分钟",(int)allHour,(RQStringIsNotEmpty(_RATIONKSMC)? _RATIONKSMC : @"小时"),(int)allMin];
  83. if (allHour == 0) {
  84. allTimeString = [NSString stringWithFormat:@"%d分钟",(int)allMin];
  85. } else if (allHour != 0 && allMin == 0) {
  86. allTimeString = [NSString stringWithFormat:@"%d%@",(int)allHour,(RQStringIsNotEmpty(_RATIONKSMC)? _RATIONKSMC : @"小时")];
  87. }
  88. if (dataArray.count > 0) {
  89. titleLabel.text = _titleString;//虽然可以用dic来判断 这样比较省事
  90. NSString *totalTimeString = [self getTotalTime:dataArray];
  91. contentLabel.text = [NSString stringWithFormat:@"已完成%@,共需%@",totalTimeString,allTimeString];
  92. } else {
  93. nowLabel.hidden = YES;
  94. titleLabel.text = _titleString;
  95. progressLabel.text = @"0%";
  96. contentLabel.text = [NSString stringWithFormat:@"已完成0分钟,共需%@",allTimeString];
  97. }
  98. }
  99. - (NSString *)getTotalTime:(NSMutableArray *)array {
  100. if ([array count] < 1) {
  101. return @"0分钟";
  102. }
  103. int time = 0;
  104. for (NSDictionary *dic in array) {
  105. time += [dic[@"CRI_DURATION"] intValue];//CRI_VAILD_TIME
  106. }
  107. int hour = time/(_RATIONKS? : 60);
  108. int min = time%(_RATIONKS? : 60);
  109. NSString *timeString = [NSString stringWithFormat:@"%d%@%d分钟",hour,(RQStringIsNotEmpty(_RATIONKSMC)? _RATIONKSMC : @"小时"),min];
  110. if (hour == 0) {
  111. timeString = [NSString stringWithFormat:@"%d分钟",min];
  112. }
  113. //在这个地方给进度条赋值
  114. CGFloat progressValue = (_allNeedTime == 0)? 0.f : time*1.0/_allNeedTime*100.f;
  115. if (time >= _allNeedTime) {
  116. progressLabel.text = @"100%";
  117. } else {
  118. NSString *progressValueString = [NSString stringWithFormat:@"%f",progressValue];
  119. progressLabel.text = [NSString stringWithFormat:@"%@%%",[progressValueString componentsSeparatedByString:@"."].firstObject];
  120. }
  121. nowLabel.hidden = NO;
  122. if (progressValue > 100.0) {
  123. nowLabel.width = allLabel.width;
  124. } else {
  125. nowLabel.width = allLabel.width * progressValue/100.0;
  126. }
  127. return timeString;
  128. }
  129. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  130. [super setSelected:selected animated:animated];
  131. }
  132. @end