No Description

FKProAmazonScoreCell.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // FKProAmazonScoreCell.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 2017/1/16.
  6. // Copyright © 2017年 FirstLink. All rights reserved.
  7. //
  8. #import "FKProAmazonScoreCell.h"
  9. #import "FKProDetailViewModel.h"
  10. @interface FKProAmazonScoreCell ()
  11. @property (nonatomic, strong) UIImageView *bestSellerView;
  12. @property (nonatomic, strong) UILabel *rankLabel;
  13. @property (nonatomic, strong) UILabel *reviewCountLabel;
  14. @property (nonatomic, strong) UILabel *amazonTitleLabel;
  15. @property (nonatomic, strong) NSMutableArray *amazonStarArray;
  16. @end
  17. static NSInteger const AMAZON_STAR_COUNT = 5;
  18. @implementation FKProAmazonScoreCell
  19. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  20. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  21. [self addAllSubviews];
  22. self.selectionStyle = UITableViewCellSelectionStyleNone;
  23. self.contentView.backgroundColor = [UIColor whiteColor];
  24. }
  25. return self;
  26. }
  27. - (void)fk_configWithViewModel:(FKProDetailViewModel *)viewModel indexPath:(NSIndexPath *)indexPath {
  28. if ([viewModel isKindOfClass:[FKProDetailViewModel class]]) {
  29. FKProductInfoItem *item = viewModel.dataItem.productInfo;
  30. if (item.officialReviewCount.length > 0) {
  31. self.reviewCountLabel.text = [NSString stringWithFormat:@"%@人评", item.officialReviewCount];
  32. }
  33. NSInteger score = [item.officialScore integerValue];
  34. UIImageView *imageView = nil;
  35. for (NSInteger idx = 0; idx < AMAZON_STAR_COUNT; idx++) {
  36. imageView = self.amazonStarArray[idx];
  37. NSInteger tempIdx = (idx+1)*10;
  38. if (tempIdx <= score) {
  39. imageView.image = [UIImage imageNamed:@"amazonFullStar"];
  40. } else if (tempIdx > score && idx*10 < score) {
  41. imageView.image = [UIImage imageNamed:@"amazonHalfStar"];
  42. } else {
  43. imageView.image = [UIImage imageNamed:@"amazonEmptyStar"];
  44. }
  45. }
  46. if (item.officialRank) {
  47. self.bestSellerView.hidden = NO;
  48. self.rankLabel.text = [NSString stringWithFormat:@" #%@ Best Seller ", item.officialRank];
  49. [self.amazonTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  50. make.left.equalTo(self.bestSellerView.mas_right).offset(8);
  51. make.centerY.equalTo(self.contentView);
  52. }];
  53. } else {
  54. self.bestSellerView.hidden = YES;
  55. [self.amazonTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  56. make.left.equalTo(self.contentView).offset(15);
  57. make.centerY.equalTo(self.contentView);
  58. }];
  59. }
  60. }
  61. }
  62. #pragma mark - Method
  63. + (CGFloat)height {
  64. return 30;
  65. }
  66. #pragma mark - Layout
  67. - (void)addAllSubviews {
  68. [self.contentView addSubview:self.bestSellerView];
  69. [self.bestSellerView mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.left.equalTo(self.contentView).offset(16);
  71. make.centerY.equalTo(self.contentView);
  72. }];
  73. [self.bestSellerView addSubview:self.rankLabel];
  74. [self.rankLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  75. make.left.top.bottom.equalTo(self.bestSellerView);
  76. make.right.equalTo(self.bestSellerView).offset(-8);
  77. }];
  78. [self.contentView addSubview:self.amazonTitleLabel];
  79. [self.amazonTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.left.equalTo(self.bestSellerView.mas_right).offset(8);
  81. make.centerY.equalTo(self.contentView);
  82. }];
  83. self.amazonStarArray = [NSMutableArray array];
  84. for (NSInteger index = 0; index < AMAZON_STAR_COUNT; index++) {
  85. [self.amazonStarArray addObject:[UIImageView new]];
  86. }
  87. UIView *preView = self.amazonTitleLabel;
  88. for (UIView *view in self.amazonStarArray) {
  89. [self.contentView addSubview:view];
  90. CGFloat offset = (preView == self.amazonTitleLabel ? 8 : 4);
  91. [view mas_makeConstraints:^(MASConstraintMaker *make) {
  92. make.left.equalTo(preView.mas_right).offset(offset);
  93. make.centerY.equalTo(preView);
  94. make.size.mas_equalTo(CGSizeMake(12, 11));
  95. }];
  96. preView = view;
  97. }
  98. UIView *lastStarView = self.amazonStarArray.lastObject;
  99. [self.contentView addSubview:self.reviewCountLabel];
  100. [self.reviewCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  101. make.left.equalTo(lastStarView.mas_right).offset(12);
  102. make.centerY.equalTo(self.contentView);
  103. }];
  104. }
  105. #pragma mark - Property
  106. - (UIImageView *)bestSellerView {
  107. if (!_bestSellerView) {
  108. _bestSellerView = [UIImageView new];
  109. _bestSellerView.image = [UIImage imageNamed:@"amazonBestSeller"];
  110. }
  111. return _bestSellerView;
  112. }
  113. - (UILabel *)rankLabel {
  114. if (!_rankLabel) {
  115. _rankLabel = [UILabel new];
  116. _rankLabel.textColor = UIColorFromRGB(0xffffff);
  117. _rankLabel.font = [UIFont boldSystemFontOfSize:11];
  118. [_rankLabel setAdjustsFontSizeToFitWidth:YES];
  119. }
  120. return _rankLabel;
  121. }
  122. - (UILabel *)reviewCountLabel {
  123. if (!_reviewCountLabel) {
  124. _reviewCountLabel = [UILabel new];
  125. _reviewCountLabel.textColor = UIColorFromRGB(0xe77800);
  126. _reviewCountLabel.font = [UIFont systemFontOfSize:11];
  127. }
  128. return _reviewCountLabel;
  129. }
  130. - (UILabel *)amazonTitleLabel {
  131. if (!_amazonTitleLabel) {
  132. _amazonTitleLabel = [UILabel new];
  133. _amazonTitleLabel.textColor = UIColorFromRGB(0x333333);
  134. _amazonTitleLabel.font = [UIFont systemFontOfSize:11];
  135. _amazonTitleLabel.text = @"亚马逊评分";
  136. }
  137. return _amazonTitleLabel;
  138. }
  139. @end