口袋优选

KBNineNineTableViewCell.m 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // KBNineNineTableViewCell.m
  3. // YouHuiProject
  4. //
  5. // Created by xiaoxi on 2018/1/17.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBNineNineTableViewCell.h"
  9. #import "KBCollectionView.h"
  10. static NSString *const cellID = @"YHNineNineTableCollectionViewCell";
  11. @implementation YHNineNineTableCollectionViewCell
  12. - (instancetype)initWithFrame:(CGRect)frame {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. [self initSubviews];
  16. }
  17. return self;
  18. }
  19. - (void)initSubviews {
  20. [self.contentView.layer addSublayer:self.pictureLayer];
  21. [self.contentView addSubview:self.topLabel];
  22. [self.contentView addSubview:self.bottomLabel];
  23. [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  24. make.left.equalTo(self.contentView);
  25. make.top.equalTo(self.contentView).offset(FITSIZE((120+5)));
  26. make.right.equalTo(self.contentView);
  27. }];
  28. [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.left.equalTo(self.contentView);
  30. make.bottom.equalTo(self.contentView).offset(-FITSIZE(10));
  31. }];
  32. }
  33. - (void)setModel:(YHNineNineMiddleCollectionModel *)model {
  34. _model = model;
  35. [self.pictureLayer yy_setImageWithURL:[NSURL URLWithString:model.img] placeholder:Placehold_Img options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation completion:nil];
  36. NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
  37. [paraStyle setLineSpacing:5];
  38. NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:model.title attributes:@{NSParagraphStyleAttributeName:paraStyle}];
  39. self.topLabel.attributedText = attString;
  40. self.bottomLabel.text = [NSString stringWithFormat:@"¥ %@", model.discount_price];
  41. }
  42. #pragma mark -lazy
  43. - (CALayer *)pictureLayer {
  44. if (!_pictureLayer) {
  45. _pictureLayer = [CALayer layer];
  46. _pictureLayer.backgroundColor = [UIColor clearColor].CGColor;
  47. _pictureLayer.frame = CGRectMake(0, 0, FITSIZE(120), FITSIZE(120));
  48. _pictureLayer.cornerRadius = 3;
  49. }
  50. return _pictureLayer;
  51. }
  52. - (UILabel *)topLabel {
  53. if (!_topLabel) {
  54. _topLabel = [[UILabel alloc] init];
  55. _topLabel.backgroundColor = [UIColor clearColor];
  56. _topLabel.textColor = [UIColor YHColorWithHex:0x444444];
  57. _topLabel.font = [UIFont systemFontOfSize:FITSIZE(12)];
  58. _topLabel.numberOfLines = 2;
  59. }
  60. return _topLabel;
  61. }
  62. - (UILabel *)bottomLabel {
  63. if (!_bottomLabel) {
  64. _bottomLabel = [[UILabel alloc] init];
  65. _bottomLabel.backgroundColor = [UIColor clearColor];
  66. _bottomLabel.textColor = [UIColor YHColorWithHex:0xff2420];
  67. _bottomLabel.font = [UIFont systemFontOfSize:FITSIZE(13)];
  68. }
  69. return _bottomLabel;
  70. }
  71. @end
  72. @interface KBNineNineTableViewCell () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  73. @end
  74. @implementation KBNineNineTableViewCell
  75. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  76. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  77. if (self) {
  78. [self initSubviews];
  79. }
  80. return self;
  81. }
  82. - (void)initSubviews {
  83. [self.contentView.layer addSublayer:self.bottomLine];
  84. [self.contentView addSubview:self.leftLabel];
  85. [self.contentView addSubview:self.rightLabel];
  86. [self.contentView addSubview:self.rightImageView];
  87. [self.contentView addSubview:self.collectionView];
  88. [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  89. make.left.equalTo(self.contentView).offset(FITSIZE(15));
  90. make.top.equalTo(self.contentView).offset(FITSIZE(12));
  91. }];
  92. [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  93. make.right.equalTo(self.contentView).offset(-FITSIZE(15));
  94. make.centerY.equalTo(self.leftLabel).offset(1);
  95. make.size.mas_equalTo(CGSizeMake(FITSIZE(20), FITSIZE(20)));
  96. }];
  97. [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  98. make.right.equalTo(self.rightImageView.mas_left);
  99. make.centerY.equalTo(self.leftLabel);
  100. }];
  101. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  102. make.left.equalTo(self.contentView);
  103. make.top.equalTo(self.leftLabel.mas_bottom).offset(FITSIZE(9));
  104. make.right.equalTo(self.contentView);
  105. make.bottom.equalTo(self.contentView);
  106. }];
  107. }
  108. - (void)setModel:(YHNineNineMiddleModel *)model {
  109. _model = model;
  110. self.leftLabel.text = model.title.name;
  111. self.rightLabel.text = [NSString stringWithFormat:@"共%@件", model.title.num];
  112. [self.collectionView reloadData];
  113. }
  114. #pragma mark - collectionView
  115. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  116. return 1;
  117. }
  118. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  119. NSArray *arr = self.model.list;
  120. return arr.count;
  121. }
  122. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  123. YHNineNineTableCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  124. YHNineNineMiddleCollectionModel *model = self.model.list[indexPath.item];
  125. cell.model = model;
  126. return cell;
  127. }
  128. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  129. if ([self.delegate respondsToSelector:@selector(yh_NineNineTableViewCellDidSelectItem:)]) {
  130. [self.delegate yh_NineNineTableViewCellDidSelectItem:self.model];
  131. }
  132. }
  133. #pragma mark - lazy
  134. - (CALayer *)bottomLine {
  135. if (!_bottomLine) {
  136. _bottomLine = [CALayer layer];
  137. _bottomLine.backgroundColor = [UIColor YHColorWithHex:0xf5f4f4].CGColor;
  138. _bottomLine.frame = CGRectMake(0, FITSIZE(235), kScreenWidth, FITSIZE(5));
  139. }
  140. return _bottomLine;
  141. }
  142. - (UILabel *)leftLabel {
  143. if (!_leftLabel) {
  144. _leftLabel = [[UILabel alloc] init];
  145. _leftLabel.backgroundColor = [UIColor clearColor];
  146. _leftLabel.textColor = [UIColor YHColorWithHex:0x222222];
  147. _leftLabel.font = [UIFont boldSystemFontOfSize:FITSIZE(14)];
  148. }
  149. return _leftLabel;
  150. }
  151. - (UILabel *)rightLabel {
  152. if (!_rightLabel) {
  153. _rightLabel = [[UILabel alloc] init];
  154. _rightLabel.backgroundColor = [UIColor clearColor];
  155. _rightLabel.textColor = [UIColor YHColorWithHex:0x999999];
  156. _rightLabel.font = [UIFont systemFontOfSize:FITSIZE(12)];
  157. _rightLabel.textAlignment = NSTextAlignmentRight;
  158. }
  159. return _rightLabel;
  160. }
  161. - (UIImageView *)rightImageView {
  162. if (!_rightImageView) {
  163. _rightImageView = [[UIImageView alloc] init];
  164. _rightImageView.backgroundColor = [UIColor clearColor];
  165. _rightImageView.image = [UIImage imageNamed:@"goto"];
  166. }
  167. return _rightImageView;
  168. }
  169. - (UICollectionView *)collectionView {
  170. if (!_collectionView) {
  171. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  172. flowLayout.itemSize = CGSizeMake(FITSIZE(120), FITSIZE(191.5));
  173. flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  174. flowLayout.minimumLineSpacing = FITSIZE(10);
  175. flowLayout.minimumInteritemSpacing = FITSIZE(0);
  176. flowLayout.sectionInset = UIEdgeInsetsMake(0, FITSIZE(15), 0, FITSIZE(15));
  177. _collectionView = [[KBCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
  178. _collectionView.contentInset = UIEdgeInsetsZero;
  179. _collectionView.showsHorizontalScrollIndicator = NO;
  180. _collectionView.delegate = self;
  181. _collectionView.dataSource = self;
  182. [_collectionView registerClass:[YHNineNineTableCollectionViewCell class] forCellWithReuseIdentifier:cellID];
  183. }
  184. return _collectionView;
  185. }
  186. @end