XYWishCenterTableViewCell.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // XYWishCenterTableViewCell.m
  3. // Timi
  4. //
  5. // Created by 翟玉磊 on 2021/10/19.
  6. //
  7. #import "XYWishCenterTableViewCell.h"
  8. #import "AWRichText.h"
  9. #import "XYWishGiftModel.h"
  10. @interface XYWishCenterTableViewCell ()
  11. @property (nonatomic, strong) UIView *infoView;
  12. @property (nonatomic, strong) UIImageView *giftImageView;
  13. @property (nonatomic, strong) UILabel *giftNameLabel;
  14. @property (nonatomic, strong) AWRichTextLabel *rewardLabel;
  15. @property (nonatomic, strong) UISwitch *wishSwitch;
  16. @property (nonatomic, strong) UILabel *completionTimeBeforeLabel;
  17. @property (nonatomic, strong) UILabel *completionTimeAfterLabel;
  18. @property (nonatomic, strong) NSIndexPath *indexPath;
  19. @end
  20. @implementation XYWishCenterTableViewCell
  21. + (instancetype)cellWithTableView:(UITableView *)tableView {
  22. XYWishCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:XYWishCenterTableViewCell.className];
  23. if (cell == nil) {
  24. cell = [[XYWishCenterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XYWishCenterTableViewCell.className];
  25. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  26. }
  27. return cell;
  28. }
  29. - (void)configureModel:(XYWishGiftModel*)model {
  30. [self.giftImageView sd_setImageWithURL:UrlForString(model.giftIcon) placeholderImage:placeholderImage()];
  31. self.giftNameLabel.text = [NSString stringWithFormat:@"%@ (%ld/%ld)", model.giftName, model.currentNum, model.giftNum];
  32. /// 状态:0关闭 1开启 2完成
  33. if (model.status == 0 || model.status == 1) {
  34. // 许愿中心
  35. self.wishSwitch.hidden = NO;
  36. self.wishSwitch.on = model.status;
  37. self.wishSwitch.enabled = YES;
  38. self.completionTimeBeforeLabel.hidden = YES;
  39. self.completionTimeAfterLabel.hidden = YES;
  40. }else {
  41. // 完成记录
  42. self.wishSwitch.hidden = YES;
  43. self.completionTimeBeforeLabel.hidden = NO;
  44. self.completionTimeAfterLabel.hidden = NO;
  45. self.completionTimeBeforeLabel.text = [SystemTimeObject timestampSwitchTime:model.completeTime andFormatter:@"YYYY/MM/dd"];
  46. self.completionTimeAfterLabel.text = [SystemTimeObject timestampSwitchTime:model.completeTime andFormatter:@"HH:mm:ss"];
  47. }
  48. [self buildRewardRichText:model];
  49. }
  50. - (void)buildRewardRichText:(XYWishGiftModel*)model {
  51. if (_rewardLabel) {
  52. [_rewardLabel removeFromSuperview];
  53. _rewardLabel = nil;
  54. }
  55. AWRichText *richText = [[AWRichText alloc] init];
  56. AWRTTextComponent *rewardTitleTextComp = [[AWRTTextComponent alloc] init]
  57. .AWText(@"奖励:")
  58. .AWFont(Font(12))
  59. .AWColor(Color_TextFont);
  60. [richText addComponent:rewardTitleTextComp];
  61. if (model.wishRewards.count > 0) {
  62. for (XYWishRewardItemModel *rewardModel in model.wishRewards) {
  63. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)];
  64. [imageView sd_setImageWithURL:UrlForString(rewardModel.rewardIcon) placeholderImage:placeholderImage()];
  65. //根据viwe创建ViewComponent,View类型也请设置font,否则可能显示异常
  66. //另外 AWRTxxxComponent组件也可以从Pool中取,直接调用addComponentFromPoolWithType:方法。
  67. //此种方法适合AWRichText的components变化频繁的情况。
  68. //正常情况使用 alloc init的方式生成即可。
  69. ((AWRTViewComponent *)[richText addComponentFromPoolWithType:AWRTComponentTypeView])
  70. .AWView(imageView)
  71. .AWFont(Font(12))
  72. .AWBoundsDepend(@(AWRTAttchmentBoundsDependContent))
  73. .AWAlignment(@(AWRTAttachmentAlignCenter));
  74. AWRTTextComponent *rewardCountTextComp = [[AWRTTextComponent alloc] init]
  75. .AWText([NSString stringWithFormat:@"*%ld", (long)rewardModel.rewardNum])
  76. .AWFont(Font(12))
  77. .AWColor(Color_TextFont);
  78. [richText addComponent:rewardCountTextComp];
  79. }
  80. }else {
  81. AWRTTextComponent *rewardCountTextComp = [[AWRTTextComponent alloc] init]
  82. .AWText(@"无")
  83. .AWFont(Font(12))
  84. .AWColor(Color_TextFont);
  85. [richText addComponent:rewardCountTextComp];
  86. }
  87. self.rewardLabel = [richText createRichTextLabel];
  88. self.rewardLabel.backgroundColor = Color_Clear;
  89. [self.infoView addSubview:self.rewardLabel];
  90. [self.rewardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.left.equalTo(self.giftImageView.mas_right).offset(8.0f);
  92. make.top.equalTo(self.giftNameLabel.mas_bottom).offset(2.0f);
  93. make.height.equalTo(@20.0f);
  94. make.right.equalTo(self.wishSwitch.mas_left).offset(-10.0f);
  95. }];
  96. }
  97. - (void)setIndexPath:(NSIndexPath *)indexPath rowsInSection:(NSInteger)rows {
  98. self.indexPath = indexPath;
  99. }
  100. - (void)wishSwitchAction:(UISwitch *)sender {
  101. if (self.delegate && [self.delegate respondsToSelector:@selector(wishCenterGiftSwitchValueChangeAtIndexPath:)]) {
  102. sender.enabled = NO;
  103. [self.delegate wishCenterGiftSwitchValueChangeAtIndexPath:self.indexPath];
  104. }
  105. }
  106. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  107. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  108. [self setupUI];
  109. }
  110. return self;
  111. }
  112. - (void)setupUI {
  113. self.backgroundColor = self.contentView.backgroundColor = Color_Clear;
  114. [self.contentView addSubview:self.infoView];
  115. [self.infoView addSubview:self.giftImageView];
  116. [self.infoView addSubview:self.giftNameLabel];
  117. [self.infoView addSubview:self.wishSwitch];
  118. [self.infoView addSubview:self.completionTimeBeforeLabel];
  119. [self.infoView addSubview:self.completionTimeAfterLabel];
  120. [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
  121. make.left.top.equalTo(self.contentView).offset(16.0f);
  122. make.right.equalTo(self.contentView).offset(-16.0f);
  123. make.bottom.equalTo(self.contentView);
  124. }];
  125. [self.giftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  126. make.left.equalTo(self.infoView).offset(16.0f);
  127. make.centerY.equalTo(self.infoView);
  128. make.width.height.equalTo(@56.0f);
  129. }];
  130. [self.giftNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  131. make.left.equalTo(self.giftImageView.mas_right).offset(8.0f);
  132. make.top.equalTo(self.infoView).offset(17.0f);
  133. make.height.equalTo(@20.0f);
  134. make.right.equalTo(self.wishSwitch.mas_left).offset(-10.0f);
  135. }];
  136. [self.wishSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
  137. make.right.equalTo(self.infoView).offset(-16.0f);
  138. make.centerY.equalTo(self.infoView);
  139. make.width.equalTo(@50.0f);
  140. make.height.equalTo(@30.0f);
  141. }];
  142. [self.completionTimeBeforeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  143. make.right.equalTo(self.infoView).offset(-16.0f);
  144. make.top.equalTo(self.infoView).offset(21.0f);
  145. make.height.equalTo(@17.0f);
  146. }];
  147. [self.completionTimeAfterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  148. make.right.equalTo(self.infoView).offset(-16.0f);
  149. make.top.equalTo(self.completionTimeBeforeLabel.mas_bottom).offset(4.0f);
  150. make.height.equalTo(@17.0f);
  151. }];
  152. [self.giftImageView addViewBorder:Color_Clear redian:56/2];
  153. [self.infoView addViewBorder:Color_Clear redian:8];
  154. }
  155. #pragma mark - Lazy
  156. - (UIView *)infoView {
  157. if (_infoView == nil) {
  158. _infoView = [UIView new];
  159. _infoView.backgroundColor = Color_White;
  160. }
  161. return _infoView;
  162. }
  163. - (UIImageView *)giftImageView {
  164. if (_giftImageView == nil) {
  165. _giftImageView = [UIImageView new];
  166. _giftImageView.image = placeholderImage();
  167. _giftImageView.clipsToBounds = YES;
  168. _giftImageView.contentMode = UIViewContentModeScaleAspectFill;
  169. }
  170. return _giftImageView;
  171. }
  172. - (UILabel *)giftNameLabel {
  173. if (_giftNameLabel == nil) {
  174. _giftNameLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font_B(14)];
  175. }
  176. return _giftNameLabel;
  177. }
  178. - (UISwitch *)wishSwitch {
  179. if (_wishSwitch == nil) {
  180. _wishSwitch = [[UISwitch alloc] init];
  181. _wishSwitch.onTintColor = Color_TextRed;
  182. _wishSwitch.thumbTintColor = Color_Background;
  183. [_wishSwitch addTarget:self action:@selector(wishSwitchAction:) forControlEvents:UIControlEventValueChanged];
  184. }
  185. return _wishSwitch;
  186. }
  187. - (UILabel *)completionTimeBeforeLabel {
  188. if (_completionTimeBeforeLabel == nil) {
  189. _completionTimeBeforeLabel = [UILabel createLabelTextColor:Color_TextGray fount:Font(12)];
  190. _completionTimeBeforeLabel.textAlignment = NSTextAlignmentRight;
  191. }
  192. return _completionTimeBeforeLabel;
  193. }
  194. - (UILabel *)completionTimeAfterLabel {
  195. if (_completionTimeAfterLabel == nil) {
  196. _completionTimeAfterLabel = [UILabel createLabelTextColor:Color_TextGray fount:Font(12)];
  197. _completionTimeAfterLabel.textAlignment = NSTextAlignmentRight;
  198. }
  199. return _completionTimeAfterLabel;
  200. }
  201. - (void)awakeFromNib {
  202. [super awakeFromNib];
  203. // Initialization code
  204. }
  205. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  206. [super setSelected:selected animated:animated];
  207. // Configure the view for the selected state
  208. }
  209. @end