Sin descripción

FKRecoLimitBuyCell.m 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // FKRecommendlimitBuyCell.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/11/30.
  6. // Copyright © 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKRecoLimitBuyCell.h"
  9. @interface FKLimitBuySingleCell ()
  10. @end
  11. @implementation FKLimitBuySingleCell
  12. - (instancetype)initWithFrame:(CGRect)frame{
  13. if (self = [super initWithFrame:frame]) {
  14. [self addAllSubviews];
  15. }
  16. return self;
  17. }
  18. - (void)addAllSubviews {
  19. [self.contentView addSubview:self.imageView];
  20. [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  21. make.top.equalTo(self.contentView);
  22. make.centerX.equalTo(self.contentView);
  23. make.left.equalTo(self.contentView).offset(6);
  24. make.right.equalTo(self.contentView).offset(-6);
  25. make.height.equalTo(self.imageView.mas_width);
  26. }];
  27. [self.imageView addSubview:self.statusImageView];
  28. [self.statusImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.center.equalTo(self.imageView);
  30. }];
  31. [self.contentView addSubview:self.priceLabel];
  32. [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  33. make.top.equalTo(self.imageView.mas_bottom).offset(4);
  34. make.left.right.equalTo(self.contentView);
  35. }];
  36. [self.contentView addSubview:self.buyButton];
  37. [self.buyButton mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.top.equalTo(self.priceLabel.mas_bottom).offset(8);
  39. make.centerX.equalTo(self.contentView);
  40. make.height.mas_equalTo(18);
  41. make.width.mas_equalTo(65);
  42. }];
  43. }
  44. #pragma mark - property
  45. - (UIImageView *)imageView {
  46. if (!_imageView) {
  47. _imageView = [[UIImageView alloc]init];
  48. _imageView.userInteractionEnabled = YES;
  49. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  50. }
  51. return _imageView;
  52. }
  53. - (UIImageView *)statusImageView {
  54. if (!_statusImageView) {
  55. _statusImageView = [[UIImageView alloc] init];
  56. _statusImageView.image = [UIImage imageNamed:@"SoldOutIcon"];
  57. _statusImageView.hidden = YES;
  58. }
  59. return _statusImageView;
  60. }
  61. - (UILabel *)priceLabel {
  62. if (!_priceLabel) {
  63. _priceLabel = [[UILabel alloc]init];
  64. _priceLabel.textAlignment = NSTextAlignmentCenter;
  65. }
  66. return _priceLabel;
  67. }
  68. - (UIButton *)buyButton {
  69. if (!_buyButton) {
  70. _buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
  71. _buyButton.userInteractionEnabled = NO;
  72. _buyButton.layer.cornerRadius = 9;
  73. _buyButton.layer.borderWidth = 1.0/[UIScreen mainScreen].scale;
  74. _buyButton.layer.borderColor = UIColorFromRGB(0xff6362).CGColor;
  75. [_buyButton setTitle:@"立即抢购" forState:UIControlStateNormal];
  76. [_buyButton setTitleColor:UIColorFromRGB(0xff624a) forState:UIControlStateNormal];
  77. [_buyButton.titleLabel setFont:[UIFont systemFontOfSize:11]];
  78. }
  79. return _buyButton;
  80. }
  81. @end
  82. #import "FKRecommendViewModel.h"
  83. @interface FKRecoLimitBuyCell ()
  84. <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
  85. @property (nonatomic, strong) UICollectionView *collectionView;
  86. @property (nonatomic, weak ) FKRecAreaItem *dataSource;
  87. @end
  88. static const NSInteger LIMITELEMENTCOUNT = 3;
  89. @implementation FKRecoLimitBuyCell
  90. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  91. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  92. self.selectionStyle = UITableViewCellSelectionStyleNone;
  93. [self addAllSubviews];
  94. }
  95. return self;
  96. }
  97. - (void)fk_configWithViewModel:(FKRecommendViewModel *)viewModel indexPath:(NSIndexPath *)indexPath {
  98. if ([viewModel isKindOfClass:[FKRecommendViewModel class]]) {
  99. self.dataSource = viewModel.limitBuyArea;
  100. [self.collectionView reloadData];
  101. }
  102. }
  103. #pragma mark - UICollectionViewDataSource
  104. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  105. return self.dataSource.dataArray.count;
  106. }
  107. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  108. return CGSizeMake([FKRecoLimitBuyCell width:self.dataSource.dataArray.count], [FKRecoLimitBuyCell height:self.dataSource.dataArray.count]);
  109. }
  110. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  111. FKLimitBuySingleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FKLimitBuySingleCell class])
  112. forIndexPath:indexPath];
  113. if (indexPath.row < self.dataSource.dataArray.count) {
  114. FKLimitBuyItem *item = self.dataSource.dataArray[indexPath.row];
  115. [cell.imageView setImageWithURL:item.photoURL cdnWidth:[FKRecoLimitBuyCell width:self.dataSource.dataArray.count]];
  116. cell.statusImageView.hidden = [item.salable isEqualToString:@"1"];
  117. NSString *price = item.price ? [NSString stringWithFormat:@"¥%@ ", [FLStringHelper convertFenToYuan:item.price]] : nil;
  118. NSString *originalPrice = item.originalPrice ? [NSString stringWithFormat:@"¥%@", [FLStringHelper convertFenToYuan:item.originalPrice]] : nil;
  119. NSAttributedString *attributeString = [FLStringHelper attStringWithText:price
  120. color:UIColorFromRGB(0x333333)
  121. font:[UIFont boldSystemFontOfSize:12]
  122. subText:originalPrice
  123. subColor:UIColorFromRGB(0x999999)
  124. subFont:[UIFont systemFontOfSize:9]];
  125. if (attributeString) {
  126. cell.priceLabel.attributedText = attributeString;
  127. }
  128. }
  129. return cell;
  130. }
  131. - (IBAction)clickLargeImageViewGesture:(UIGestureRecognizer *)gesture {
  132. if (self.delegate && [self.delegate respondsToSelector:@selector(recommendClickCell:atIndexPath:offset:)]) {
  133. [self.delegate recommendClickCell:self atIndexPath:self.indexPath offset:gesture.view.tag];
  134. }
  135. }
  136. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  137. if (self.delegate && [self.delegate respondsToSelector:@selector(recommendClickCell:atIndexPath:offset:)]) {
  138. [self.delegate recommendClickCell:self atIndexPath:self.indexPath offset:indexPath.row];
  139. }
  140. }
  141. #pragma mark - Action
  142. - (IBAction)clickViewGesture:(UIGestureRecognizer *)gesture {
  143. if (self.delegate && [self.delegate respondsToSelector:@selector(recommendClickCell:atIndexPath:offset:)]) {
  144. [self.delegate recommendClickCell:self atIndexPath:self.indexPath offset:gesture.view.tag];
  145. }
  146. }
  147. #pragma mark - Layout
  148. - (void)addAllSubviews {
  149. [self.contentView addSubview:self.collectionView];
  150. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  151. make.edges.equalTo(self.contentView);
  152. }];
  153. }
  154. + (CGFloat)width:(CGFloat)itemCount {
  155. CGFloat count = 3.5;
  156. if (IS_IPHONE_4 || IS_IPHONE_5) {
  157. count = 2.7;
  158. }
  159. if (itemCount <= LIMITELEMENTCOUNT) {
  160. count = LIMITELEMENTCOUNT;
  161. }
  162. return (int)(UISCREENWIDTH/count);
  163. }
  164. + (CGFloat)height:(CGFloat)itemCount {
  165. CGFloat width = [FKRecoLimitBuyCell width:itemCount];
  166. CGFloat height = (width - 6 - 6);
  167. return (height + 58);
  168. }
  169. #pragma mark - Property
  170. - (UICollectionView *)collectionView {
  171. if (!_collectionView) {
  172. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  173. layout.headerReferenceSize = CGSizeZero;
  174. layout.footerReferenceSize = CGSizeZero;
  175. layout.minimumInteritemSpacing = 0;
  176. layout.minimumLineSpacing = 0;
  177. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  178. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  179. _collectionView.dataSource = self;
  180. _collectionView.delegate = self;
  181. _collectionView.backgroundColor = [UIColor whiteColor];
  182. _collectionView.alwaysBounceVertical = NO;
  183. _collectionView.showsHorizontalScrollIndicator = NO;
  184. _collectionView.scrollsToTop = NO;
  185. [_collectionView registerClass:[FKLimitBuySingleCell class] forCellWithReuseIdentifier:NSStringFromClass([FKLimitBuySingleCell class])];
  186. }
  187. return _collectionView;
  188. }
  189. @end