No Description

ProductDetailTableCell.m 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // PdProductDetailTableViewCell.m
  3. // FirstLink
  4. //
  5. // Created by 王孝道 on 15/6/1.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "ProductDetailTableCell.h"
  9. @interface ProductDetailTableCell ()
  10. @property (nonatomic, strong) UIButton *addBtn;
  11. @property (nonatomic, strong) UIButton *reduceBtn;
  12. @property (nonatomic, strong) UILabel *numberLabel;
  13. @property (nonatomic, strong) UILabel *groupNumberLabel; // 团购时用来显示数量
  14. @end
  15. @implementation ProductDetailTableCell
  16. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  17. {
  18. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  19. if (self) {
  20. [self initialize];
  21. self.selectionStyle = UITableViewCellSelectionStyleNone;
  22. }
  23. return self;
  24. }
  25. - (void)initialize
  26. {
  27. UIView *bottomLine = [[UIView alloc]init];
  28. bottomLine.backgroundColor = UIColorFromRGB(0xe5e5e5);
  29. [self.contentView addSubview:self.productImgView];
  30. [self.contentView addSubview:self.descripLabel];
  31. [self.contentView addSubview:self.priceLabel];
  32. [self.contentView addSubview:self.specLabel];
  33. [self.contentView addSubview:self.addBtn];
  34. [self.contentView addSubview:self.reduceBtn];
  35. [self.contentView addSubview:self.numberLabel];
  36. [self.contentView addSubview:bottomLine];
  37. UIView *superView = self.contentView;
  38. [self.productImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.left.equalTo(superView).offset(15);
  40. make.centerY.equalTo(superView);
  41. make.width.height.equalTo(@70);
  42. }];
  43. [self.descripLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.left.equalTo(self.productImgView.mas_right).offset(15);
  45. make.right.lessThanOrEqualTo(self.priceLabel.mas_left).offset(- 15);
  46. make.top.equalTo(self.productImgView);
  47. }];
  48. [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  49. make.right.equalTo(superView).offset(- 15);
  50. make.top.equalTo(self.descripLabel);
  51. }];
  52. [self.specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  53. make.left.equalTo(self.descripLabel);
  54. make.top.equalTo(self.descripLabel.mas_bottom);
  55. make.right.equalTo(self.reduceBtn.mas_left).offset(- 5);
  56. make.bottom.equalTo(self.productImgView).offset(- 5);
  57. }];
  58. [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.right.equalTo(superView).offset(- 15);
  60. make.bottom.equalTo(self.productImgView);
  61. }];
  62. [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  63. make.right.equalTo(self.addBtn.mas_left).offset(- 3);
  64. make.centerY.equalTo(self.addBtn);
  65. make.width.equalTo(@60);
  66. make.height.equalTo(self.addBtn);
  67. }];
  68. [self.contentView addSubview:self.groupNumberLabel];
  69. [self.groupNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.right.equalTo(self.priceLabel.mas_right);
  71. make.top.equalTo(self.priceLabel.mas_bottom);
  72. make.width.mas_equalTo(60);
  73. make.height.mas_equalTo(20);
  74. }];
  75. [self.reduceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.right.equalTo(self.numberLabel.mas_left).offset(- 3);
  77. make.centerY.equalTo(self.addBtn);
  78. }];
  79. [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.right.bottom.equalTo(self.contentView);
  81. make.height.equalTo(@0.5);
  82. make.left.equalTo(self.contentView).offset(15);
  83. }];
  84. }
  85. + (NSString *)cdnImgUrlStringWithString:(NSString *)imgUrl
  86. {
  87. return imgUrl;
  88. // return [NSString stringWithFormat:@"%@%@", imgUrl,
  89. // [FLStringHelper cdnParamaterString:70 height:70]];
  90. }
  91. - (void)configCellForNormal {
  92. self.addBtn.hidden = NO;
  93. self.reduceBtn.hidden = NO;
  94. self.numberLabel.hidden = NO;
  95. self.groupNumberLabel.hidden = YES;
  96. }
  97. - (void)configCellForGroup {
  98. self.addBtn.hidden = YES;
  99. self.reduceBtn.hidden = YES;
  100. self.numberLabel.hidden = YES;
  101. self.groupNumberLabel.hidden = NO;
  102. }
  103. #pragma mark - action
  104. - (void)numberChangeBtnClick:(UIButton *)sender{
  105. NSUInteger number = self.numberLabel.text.integerValue;
  106. if (sender.tag == 0) {
  107. number ++;
  108. if (self.numberChangeAction) {
  109. if (!self.numberChangeAction(number, self.indexPath)) return;
  110. }
  111. self.numberLabel.text = [NSString stringWithFormat:@"%ld", (long)number];
  112. }else if (sender.tag == 1){
  113. number --;
  114. if (number > 0) self.numberLabel.text = [NSString stringWithFormat:@"%ld", (long)number];
  115. if (self.numberChangeAction) self.numberChangeAction(number, self.indexPath);
  116. }
  117. }
  118. #pragma mark - getter && setter
  119. - (UIImageView *)productImgView
  120. {
  121. if (_productImgView == nil) {
  122. _productImgView = [[UIImageView alloc]init];
  123. _productImgView.contentMode = UIViewContentModeScaleAspectFit;
  124. _productImgView.backgroundColor = [UIColor whiteColor];
  125. }
  126. return _productImgView;
  127. }
  128. - (UILabel *)descripLabel
  129. {
  130. if (_descripLabel == nil) {
  131. _descripLabel = [[UILabel alloc]init];
  132. _descripLabel.backgroundColor = [UIColor clearColor];
  133. _descripLabel.font = [UIFont systemFontOfSize:13];
  134. _descripLabel.textColor = UIColorFromRGB(0x333333);
  135. _descripLabel.numberOfLines = 2;
  136. _descripLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  137. }
  138. return _descripLabel;
  139. }
  140. - (UILabel *)priceLabel
  141. {
  142. if (_priceLabel == nil) {
  143. _priceLabel = [[UILabel alloc]init];
  144. _priceLabel.backgroundColor = [UIColor clearColor];
  145. _priceLabel.font = [UIFont systemFontOfSize:12];
  146. _priceLabel.textColor = UIColorFromRGB(0x333333);
  147. _priceLabel.textAlignment = NSTextAlignmentRight;
  148. [_priceLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
  149. }
  150. return _priceLabel;
  151. }
  152. - (UIButton *)addBtn{
  153. if (_addBtn == nil) {
  154. _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  155. [_addBtn setImage:[UIImage imageNamed:@"specification_body_add_n"] forState:UIControlStateNormal];
  156. [_addBtn addTarget:self action:@selector(numberChangeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  157. _addBtn.tag = 0;
  158. [_addBtn setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
  159. }
  160. return _addBtn;
  161. }
  162. - (UIButton *)reduceBtn{
  163. if (_reduceBtn == nil) {
  164. _reduceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  165. [_reduceBtn setImage:[UIImage imageNamed:@"specification_body_lower_n"] forState:UIControlStateNormal];
  166. [_reduceBtn addTarget:self action:@selector(numberChangeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  167. _reduceBtn.tag = 1;
  168. [_reduceBtn setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
  169. [_reduceBtn setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
  170. }
  171. return _reduceBtn;
  172. }
  173. - (UILabel *)specLabel{
  174. if (_specLabel == nil) {
  175. _specLabel = [[UILabel alloc]init];
  176. _specLabel.font = [UIFont systemFontOfSize:13];
  177. _specLabel.textColor = UIColorFromRGB(0x999999);
  178. _specLabel.numberOfLines = 0;
  179. _specLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  180. }
  181. return _specLabel;
  182. }
  183. - (UILabel *)numberLabel
  184. {
  185. if (_numberLabel == nil) {
  186. _numberLabel = [[UILabel alloc]init];
  187. _numberLabel.textColor = UIColorFromRGB(0x333333);
  188. _numberLabel.font = [UIFont systemFontOfSize:14];
  189. _numberLabel.textAlignment = NSTextAlignmentCenter;
  190. _numberLabel.layer.borderColor = [UIColorFromRGB(0x999999) CGColor];
  191. _numberLabel.layer.borderWidth = 0.5f;
  192. _numberLabel.text = @"1";
  193. }
  194. return _numberLabel;
  195. }
  196. - (UILabel *)groupNumberLabel
  197. {
  198. if (_groupNumberLabel == nil) {
  199. _groupNumberLabel = [[UILabel alloc]init];
  200. _groupNumberLabel.textColor = UIColorFromRGB(0x999999);
  201. _groupNumberLabel.font = [UIFont systemFontOfSize:12];
  202. _groupNumberLabel.textAlignment = NSTextAlignmentRight;
  203. _groupNumberLabel.hidden = YES;
  204. _groupNumberLabel.text = @"x1";
  205. }
  206. return _groupNumberLabel;
  207. }
  208. - (NSInteger )number
  209. {
  210. return self.numberLabel.text.integerValue >= 0 ? self.numberLabel.text.integerValue : 0;
  211. }
  212. - (void)setNumber:(NSInteger)number
  213. {
  214. if (number <= 0) {
  215. number = 1;
  216. }
  217. self.numberLabel.text = [NSString stringWithFormat:@"%ld", (long)number];
  218. }
  219. @end