XYMedalListTableViewCell.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // XYMedalListTableViewCell.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/1/13.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "XYMedalListTableViewCell.h"
  9. #import "XYMedalListCellModel.h"
  10. #import "CustomSwitch.h"
  11. @interface XYMedalListTableViewCell ()
  12. @property (nonatomic, strong) UIView *medalBgView;
  13. @property (nonatomic, strong) UIImageView *medalImageView;
  14. @property (nonatomic, readwrite, strong) UILabel *medalNameLabel;
  15. @property (nonatomic, readwrite, strong) UILabel *timeLabel;
  16. @property (nonatomic, readwrite, strong) UIButton *useButton;
  17. @property (nonatomic, readwrite, strong) UIView *line;
  18. @property (nonatomic, readwrite, strong) NSIndexPath *indexPath;
  19. @end
  20. @implementation XYMedalListTableViewCell
  21. #pragma mark - Public Method
  22. + (instancetype)cellWithTableView:(UITableView *)tableView {
  23. static NSString *cellId = @"XYMedalListTableViewCell";
  24. XYMedalListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  25. if (!cell) {
  26. cell = [[XYMedalListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
  27. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  28. }
  29. return cell;
  30. }
  31. - (void)configureModel:(XYMedalListCellModel *)model {
  32. [self.medalImageView sd_setImageWithURL:UrlForString(model.item.medalIcon) placeholderImage:placeholderImage()];
  33. self.medalNameLabel.text = model.item.medalName;
  34. if ([model.expireTime isEqualToString:@"-1"]) {
  35. self.timeLabel.text = @"";
  36. }else {
  37. self.timeLabel.text = [NSString stringWithFormat:@"%@", [SystemTimeObject dayHourMinuteDifferentTimeAnotherTime:model.expireTime]];
  38. }
  39. if (model.useStatus) {
  40. // 已经使用
  41. [self.useButton setTitle:kLocalizedString(@"启用中") forState:UIControlStateNormal];
  42. [self.useButton setTitleColor:Color_TextRed forState:UIControlStateNormal];
  43. [self.useButton setBackgroundImage:nil forState:UIControlStateNormal];
  44. }else {
  45. // 没有使用
  46. [self.useButton setTitle:kLocalizedString(@"启用") forState:UIControlStateNormal];
  47. [self.useButton setTitleColor:Color_White forState:UIControlStateNormal];
  48. [self.useButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[ColorFromHexString(@"#5D26FF"), ColorFromHexString(@"#9059FF")] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(54, 26)] forState:UIControlStateNormal];
  49. }
  50. }
  51. - (void)setIndexPath:(NSIndexPath *)indexPath rowsInSection:(NSInteger)rows {
  52. self.indexPath = indexPath;
  53. }
  54. #pragma mark — Action
  55. - (void)useButtonAction:(id)sender {
  56. if (self.delegate && [self.delegate respondsToSelector:@selector(swichViewDidDelayChange:atIndexPath:)]) {
  57. [self.delegate swichViewDidDelayChange:self.useButton.selected atIndexPath:self.indexPath];
  58. }
  59. }
  60. #pragma mark - 初始化
  61. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  62. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  63. // 初始化
  64. [self _setup];
  65. // 创建自控制器
  66. [self _setupSubViews];
  67. // 布局子控件
  68. [self _makeSubViewsConstraints];
  69. }
  70. return self;
  71. }
  72. #pragma mark - 事件处理Or辅助方法
  73. - (void)switchOnValueChange:(id)sender {
  74. //NSLog(@"switch:on:%ld", self.switchView.on);
  75. }
  76. #pragma mark - Private Method
  77. - (void)_setup{
  78. self.backgroundColor = UIColor.whiteColor;
  79. }
  80. #pragma mark - 创建子控件
  81. - (void)_setupSubViews{
  82. [self.contentView addSubview:self.medalBgView];
  83. [self.medalBgView addSubview:self.medalImageView];
  84. [self.contentView addSubview:self.medalNameLabel];
  85. [self.contentView addSubview:self.timeLabel];
  86. [self.contentView addSubview:self.useButton];
  87. [self.contentView addSubview:self.line];
  88. [self.useButton addTarget:self action:@selector(useButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  89. }
  90. #pragma mark - 布局子控件
  91. - (void)_makeSubViewsConstraints{
  92. [self.medalBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  93. make.left.equalTo(self.contentView).offset(15.0f);
  94. make.centerY.equalTo(self.contentView);
  95. make.width.height.equalTo(@90.0f);
  96. }];
  97. [self.medalImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  98. make.center.equalTo(self.medalBgView);
  99. make.width.height.equalTo(@66.0f);
  100. }];
  101. [self.medalNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  102. make.left.equalTo(self.medalBgView.mas_right).offset(12.0f);
  103. make.top.equalTo(self.medalBgView).offset(24.0f);
  104. make.height.equalTo(@22.0f);
  105. make.right.equalTo(self.useButton.mas_left).offset(-10.0f);
  106. }];
  107. [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  108. make.left.equalTo(self.medalBgView.mas_right).offset(12.0f);
  109. make.top.equalTo(self.medalNameLabel.mas_bottom).offset(1.0f);
  110. make.height.equalTo(@17.0f);
  111. make.right.equalTo(self.useButton.mas_left).offset(-10.0f);
  112. }];
  113. [self.useButton mas_makeConstraints:^(MASConstraintMaker *make) {
  114. make.right.equalTo(self.contentView).offset(-SPACING_EDGE);
  115. make.centerY.equalTo(self.contentView);
  116. make.width.equalTo(@54.0f);
  117. make.height.equalTo(@26.0f);
  118. }];
  119. [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
  120. make.left.equalTo(self.medalNameLabel);
  121. make.bottom.right.equalTo(self.contentView);
  122. make.height.equalTo(@0.3f);
  123. }];
  124. [self.useButton addViewBorder:Color_TextRed redian:13];
  125. [self.medalBgView addViewBorder:Color_Clear redian:4];
  126. }
  127. - (void)awakeFromNib {
  128. [super awakeFromNib];
  129. // Initialization code
  130. }
  131. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  132. [super setSelected:selected animated:animated];
  133. // Configure the view for the selected state
  134. }
  135. - (UIView *)medalBgView {
  136. if (!_medalBgView) {
  137. _medalBgView = [UIView new];
  138. _medalBgView.backgroundColor = ColorFromHexString(@"#F3F3F3");
  139. }
  140. return _medalBgView;
  141. }
  142. - (UIImageView *)medalImageView {
  143. if (!_medalImageView) {
  144. _medalImageView = [UIImageView new];
  145. _medalImageView.contentMode = UIViewContentModeScaleAspectFit;
  146. _medalImageView.clipsToBounds = YES;
  147. }
  148. return _medalImageView;
  149. }
  150. - (UILabel *)medalNameLabel {
  151. if (!_medalNameLabel) {
  152. _medalNameLabel = [UILabel createLabelTextColor:Color_Nakaguro fount:Font_B(16)];
  153. }
  154. return _medalNameLabel;
  155. }
  156. - (UILabel *)timeLabel {
  157. if (!_timeLabel) {
  158. _timeLabel = [UILabel createLabelTextColor:Color_TextRed fount:Font(12)];
  159. }
  160. return _timeLabel;
  161. }
  162. - (UIButton *)useButton {
  163. if (!_useButton) {
  164. _useButton = [UIButton buttonWithType:UIButtonTypeCustom];
  165. _useButton.titleLabel.font = Font(12);
  166. }
  167. return _useButton;
  168. }
  169. - (UIView *)line {
  170. if (!_line) {
  171. _line = [UIView new];
  172. _line.backgroundColor = Color_line;
  173. }
  174. return _line;
  175. }
  176. @end