No Description

PdProductShowCell.m 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // PdProductShowCell.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/7/17.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "PdProductShowCell.h"
  9. #import "ProductShowItem.h"
  10. #import "FKProDetailViewModel.h"
  11. @implementation PdProductShowCell
  12. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  13. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  14. if (self) {
  15. [self initialize];
  16. }
  17. self.selectionStyle = UITableViewCellSelectionStyleNone;
  18. return self;
  19. }
  20. - (void)initialize {
  21. [self.contentView addSubview:self.showImgView];
  22. [self.contentView addSubview:self.contentLabel];
  23. self.contentView.backgroundColor = [UIColor clearColor];
  24. [self.showImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  25. make.top.equalTo(self.contentView);
  26. make.centerX.equalTo(self.contentView);
  27. make.width.mas_equalTo([PdProductShowCell defaultImgWidth]);
  28. make.height.mas_equalTo([PdProductShowCell defaultImgHeight]);
  29. }];
  30. [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  31. make.left.equalTo(self.contentView).offset(15);
  32. make.right.equalTo(self.contentView).offset(- 15);
  33. make.bottom.equalTo(self.contentView).offset(- 15);
  34. }];
  35. }
  36. - (void)fk_configWithViewModel:(id)viewModel indexPath:(NSIndexPath *)indexPath{
  37. if ([viewModel isKindOfClass:[FKProDetailViewModel class]]) {
  38. FKProDetailViewModel *detailModel = (FKProDetailViewModel *)viewModel;
  39. ProductShowItem *showItem = [detailModel.dataItem productShowItemForIndex:indexPath.row];
  40. [self configWithShowItem:showItem];
  41. }
  42. }
  43. - (void)configWithShowItem:(ProductShowItem *)item{
  44. if (item) {
  45. self.showImgView.image = nil;
  46. [self.showImgView setImageWithURL:item.imageUrl cdnWidth:[item realImgSize].width];
  47. self.imgHeightCtrl.constant = [item realImgSize].height;
  48. self.imgWidthCtrl.constant = [item realImgSize].width;
  49. self.contentLabel.attributedText = [FLStringHelper attStringWithText:item.describString lineSpace:2.4f];
  50. }
  51. }
  52. + (CGFloat)defaultImgHeight {
  53. return [self defaultImgWidth] * 9.0f / 7.0f;
  54. }
  55. + (CGFloat)defaultImgWidth {
  56. return UISCREENWIDTH - 20;
  57. }
  58. + (CGFloat)cellHeightForText:(NSString *)text withImgHeight:(CGFloat)imgHeight {
  59. // CGFloat imgH = imgHeight ? imgHeight : [PdProductShowCell imgViewHeigth];
  60. if (text.length == 0) {
  61. return (imgHeight + 10);
  62. }
  63. CGFloat textWidth = [UIScreen mainScreen].bounds.size.width - 15 * 2;
  64. NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
  65. paraStyle.lineSpacing = 2.4f;
  66. NSDictionary *dict = @{NSFontAttributeName : [UIFont systemFontOfSize:14], NSParagraphStyleAttributeName : paraStyle};
  67. CGRect rect = [text boundingRectWithSize:CGSizeMake(textWidth, 0)
  68. options:NSStringDrawingUsesLineFragmentOrigin
  69. attributes:dict
  70. context:nil];
  71. return imgHeight + 15 * 2 + rect.size.height;
  72. }
  73. #pragma mark - action
  74. - (void)clickImgView{
  75. if ([self.delegate respondsToSelector:@selector(clickProductImg:)]) {
  76. [self.delegate clickProductImg:self];
  77. }
  78. }
  79. //#pragma mark - Function
  80. //
  81. //- (void)configCell:(id)obj {
  82. // if ([obj isKindOfClass:[ProductShowItem class]]) {
  83. // ProductShowItem *item = (ProductShowItem *)obj;
  84. //
  85. // self.showImgView.image = nil;
  86. // [self.showImgView setImageWithURL:[NSURL URLWithString:item.imageUrl]];
  87. // self.imgHeightCtrl.constant = [item realImgSize].height;
  88. // self.imgWidthCtrl.constant = [item realImgSize].width;
  89. // self.contentLabel.attributedText = [FLStringHelper attStringWithText:item.describString
  90. // lineSpace:2.4f];
  91. // }
  92. //}
  93. #pragma mark - getter && setter
  94. - (UIImageView *)showImgView
  95. {
  96. if (_showImgView == nil) {
  97. _showImgView = [[UIImageView alloc]init];
  98. _showImgView.userInteractionEnabled = YES;
  99. _showImgView.contentMode = UIViewContentModeScaleAspectFit;
  100. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImgView)];
  101. [_showImgView addGestureRecognizer:tap];
  102. }
  103. return _showImgView;
  104. }
  105. - (UILabel *)contentLabel
  106. {
  107. if (_contentLabel == nil) {
  108. _contentLabel = [[UILabel alloc]init];
  109. _contentLabel.backgroundColor = [UIColor clearColor];
  110. _contentLabel.numberOfLines = 0;
  111. _contentLabel.textAlignment = NSTextAlignmentLeft;
  112. _contentLabel.textColor = UIColorFromRGB(0x666666);
  113. _contentLabel.font = [UIFont systemFontOfSize:14];
  114. _contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
  115. }
  116. return _contentLabel;
  117. }
  118. - (NSLayoutConstraint *)imgHeightCtrl
  119. {
  120. if (_imgHeightCtrl == nil) {
  121. for (NSLayoutConstraint *constraint in self.showImgView.constraints) {
  122. if (constraint.firstItem == self.showImgView && constraint.firstAttribute == NSLayoutAttributeHeight){
  123. _imgHeightCtrl = constraint;
  124. break;
  125. }
  126. }
  127. }
  128. return _imgHeightCtrl;
  129. }
  130. - (NSLayoutConstraint *)imgWidthCtrl
  131. {
  132. if (_imgWidthCtrl == nil) {
  133. for (NSLayoutConstraint *constraint in self.showImgView.constraints) {
  134. if (constraint.firstItem == self.showImgView && constraint.firstAttribute == NSLayoutAttributeWidth){
  135. _imgWidthCtrl = constraint;
  136. break;
  137. }
  138. }
  139. }
  140. return _imgWidthCtrl;
  141. }
  142. @end