No Description

FKVipPrivilegeDetailCell.m 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // FKVipPrivilegeDetailCell.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 2017/5/18.
  6. // Copyright © 2017年 FirstLink. All rights reserved.
  7. //
  8. #import "FKVipPrivilegeDetailCell.h"
  9. #import "FKVipPrivilegeDetailCollectionCell.h"
  10. #import "FKVipBuyRuleItem.h"
  11. static NSString * const kLeftFixSpaceString = @" ";
  12. static NSString * const kRightFixSpaceString = @" ";
  13. @interface FKVipPrivilegeDetailCell ()
  14. <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
  15. @property (nonatomic, strong) UILabel *titleLabel;
  16. @property (nonatomic, strong) UICollectionView *collectionView;
  17. @property (nonatomic, strong) NSArray<FKVipBuyRuleItem *> *buyRuleArray;
  18. @property (nonatomic, strong) NSArray *titleArray;
  19. @end
  20. @implementation FKVipPrivilegeDetailCell
  21. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  22. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  23. if (self) {
  24. self.selectionStyle = UITableViewCellSelectionStyleNone;
  25. [self setupViews];
  26. }
  27. return self;
  28. }
  29. #pragma mark - UICollectionViewDataSource, UICollectionViewDelegate
  30. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  31. return 4 + 4*self.buyRuleArray.count;
  32. }
  33. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  34. return CGSizeMake([FKVipPrivilegeDetailCell widthForCellAtIndex:indexPath.row], [FKVipPrivilegeDetailCell heightForCellAtIndex:indexPath.row]);
  35. }
  36. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  37. FKVipPrivilegeDetailCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FKVipPrivilegeDetailCollectionCell class])
  38. forIndexPath:indexPath];
  39. if (indexPath.row < 4) {
  40. NSString *title = self.titleArray[indexPath.row];
  41. if (indexPath.row < 2) {
  42. cell.titleLabel.text = [NSString stringWithFormat:@"%@%@", kLeftFixSpaceString, title];
  43. } else {
  44. cell.titleLabel.text = [NSString stringWithFormat:@"%@%@", kRightFixSpaceString, title];
  45. }
  46. } else {
  47. NSInteger index = indexPath.row/4 - 1;
  48. if (index >= 0 && index < self.buyRuleArray.count) {
  49. FKVipBuyRuleItem *item = self.buyRuleArray[index];
  50. switch (indexPath.row%4) {
  51. case 0: {
  52. cell.titleLabel.text = [NSString stringWithFormat:@"%@%@", kLeftFixSpaceString, [item couponString]];
  53. break;
  54. }
  55. case 1: {
  56. cell.titleLabel.text = [NSString stringWithFormat:@"%@%@", kLeftFixSpaceString, [item discountString]];
  57. break;
  58. }
  59. case 2: {
  60. cell.titleLabel.text = [NSString stringWithFormat:@"%@%@", kRightFixSpaceString, [item validTimeString]];
  61. break;
  62. }
  63. case 3: {
  64. cell.titleLabel.text = [NSString stringWithFormat:@"%@%@", kRightFixSpaceString, item.realPrice];
  65. break;
  66. }
  67. default:
  68. break;
  69. }
  70. }
  71. }
  72. return cell;
  73. }
  74. #pragma mark - Action
  75. - (void)reloadData:(NSArray<FKVipBuyRuleItem *> *)buyRuleArray {
  76. self.buyRuleArray = buyRuleArray;
  77. [self.collectionView reloadData];
  78. }
  79. #pragma mark - Method
  80. + (CGFloat)widthForCellAtIndex:(NSInteger)index {
  81. if (index%4 >= 2) {
  82. return 60;
  83. }
  84. return (int)((UISCREENWIDTH - 2*18 - 2*60)/2);
  85. }
  86. + (CGFloat)heightForCellAtIndex:(NSInteger)index {
  87. if (index < 4) {
  88. return 37;
  89. }
  90. return 36;
  91. }
  92. - (CGFloat)margin {
  93. return (float)(UISCREENWIDTH - 2*[FKVipPrivilegeDetailCell widthForCellAtIndex:0] - 2*[FKVipPrivilegeDetailCell widthForCellAtIndex:3])/2.0;
  94. }
  95. - (void)setupViews {
  96. [self.contentView addSubview:self.titleLabel];
  97. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  98. make.left.equalTo(self.contentView).offset(18);
  99. make.top.equalTo(self.contentView).offset(24);
  100. }];
  101. [self.contentView addSubview:self.collectionView];
  102. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  103. make.left.equalTo(self.contentView).offset([self margin]);
  104. make.right.equalTo(self.contentView).offset(-[self margin]);
  105. make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
  106. make.height.mas_equalTo(37+36*3);
  107. }];
  108. }
  109. #pragma mark - Property
  110. - (UILabel *)titleLabel {
  111. if (!_titleLabel) {
  112. _titleLabel = [UILabel new];
  113. _titleLabel.font = [UIFont systemFontOfSize:14];
  114. _titleLabel.textColor = UIColorFromRGB(0x333333);
  115. _titleLabel.text = @"会员使用说明:";
  116. }
  117. return _titleLabel;
  118. }
  119. - (UICollectionView *)collectionView {
  120. if (!_collectionView) {
  121. UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
  122. layout.minimumLineSpacing = CGFLOAT_MIN;
  123. layout.minimumInteritemSpacing = CGFLOAT_MIN;
  124. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  125. _collectionView.showsVerticalScrollIndicator = NO;
  126. _collectionView.alwaysBounceVertical = YES;
  127. _collectionView.dataSource = self;
  128. _collectionView.delegate = self;
  129. _collectionView.backgroundColor = UIColorFromRGB(0xffffff);
  130. _collectionView.contentInset = UIEdgeInsetsZero;
  131. _collectionView.scrollEnabled = NO;
  132. [_collectionView registerClass:[FKVipPrivilegeDetailCollectionCell class]
  133. forCellWithReuseIdentifier:NSStringFromClass([FKVipPrivilegeDetailCollectionCell class])];
  134. _collectionView.layer.borderColor = UIColorFromRGB(0xe5e5e5).CGColor;
  135. _collectionView.layer.borderWidth = 1.0/[UIScreen mainScreen].scale;
  136. }
  137. return _collectionView;
  138. }
  139. - (NSArray *)titleArray {
  140. if (!_titleArray) {
  141. _titleArray = @[@"包邮券", @"会员价", @"有效期", @"价格"];
  142. }
  143. return _titleArray;
  144. }
  145. @end