123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- //
- // XYWishCenterTableViewCell.m
- // Timi
- //
- // Created by 翟玉磊 on 2021/10/19.
- //
- #import "XYWishCenterTableViewCell.h"
- #import "AWRichText.h"
- #import "XYWishGiftModel.h"
- @interface XYWishCenterTableViewCell ()
- @property (nonatomic, strong) UIView *infoView;
- @property (nonatomic, strong) UIImageView *giftImageView;
- @property (nonatomic, strong) UILabel *giftNameLabel;
- @property (nonatomic, strong) AWRichTextLabel *rewardLabel;
- @property (nonatomic, strong) UISwitch *wishSwitch;
- @property (nonatomic, strong) UILabel *completionTimeBeforeLabel;
- @property (nonatomic, strong) UILabel *completionTimeAfterLabel;
- @property (nonatomic, strong) NSIndexPath *indexPath;
- @end
- @implementation XYWishCenterTableViewCell
- + (instancetype)cellWithTableView:(UITableView *)tableView {
- XYWishCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:XYWishCenterTableViewCell.className];
- if (cell == nil) {
- cell = [[XYWishCenterTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XYWishCenterTableViewCell.className];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- }
- return cell;
- }
- - (void)configureModel:(XYWishGiftModel*)model {
-
- [self.giftImageView sd_setImageWithURL:UrlForString(model.giftIcon) placeholderImage:placeholderImage()];
- self.giftNameLabel.text = [NSString stringWithFormat:@"%@ (%ld/%ld)", model.giftName, model.currentNum, model.giftNum];
-
- /// 状态:0关闭 1开启 2完成
- if (model.status == 0 || model.status == 1) {
- // 许愿中心
- self.wishSwitch.hidden = NO;
- self.wishSwitch.on = model.status;
- self.wishSwitch.enabled = YES;
- self.completionTimeBeforeLabel.hidden = YES;
- self.completionTimeAfterLabel.hidden = YES;
- }else {
- // 完成记录
- self.wishSwitch.hidden = YES;
- self.completionTimeBeforeLabel.hidden = NO;
- self.completionTimeAfterLabel.hidden = NO;
- self.completionTimeBeforeLabel.text = [SystemTimeObject timestampSwitchTime:model.completeTime andFormatter:@"YYYY/MM/dd"];
- self.completionTimeAfterLabel.text = [SystemTimeObject timestampSwitchTime:model.completeTime andFormatter:@"HH:mm:ss"];
- }
-
- [self buildRewardRichText:model];
- }
- - (void)buildRewardRichText:(XYWishGiftModel*)model {
- if (_rewardLabel) {
- [_rewardLabel removeFromSuperview];
- _rewardLabel = nil;
- }
-
- AWRichText *richText = [[AWRichText alloc] init];
-
- AWRTTextComponent *rewardTitleTextComp = [[AWRTTextComponent alloc] init]
- .AWText(@"奖励:")
- .AWFont(Font(12))
- .AWColor(Color_TextFont);
- [richText addComponent:rewardTitleTextComp];
-
- if (model.wishRewards.count > 0) {
- for (XYWishRewardItemModel *rewardModel in model.wishRewards) {
- UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)];
- [imageView sd_setImageWithURL:UrlForString(rewardModel.rewardIcon) placeholderImage:placeholderImage()];
-
- //根据viwe创建ViewComponent,View类型也请设置font,否则可能显示异常
- //另外 AWRTxxxComponent组件也可以从Pool中取,直接调用addComponentFromPoolWithType:方法。
- //此种方法适合AWRichText的components变化频繁的情况。
- //正常情况使用 alloc init的方式生成即可。
- ((AWRTViewComponent *)[richText addComponentFromPoolWithType:AWRTComponentTypeView])
- .AWView(imageView)
- .AWFont(Font(12))
- .AWBoundsDepend(@(AWRTAttchmentBoundsDependContent))
- .AWAlignment(@(AWRTAttachmentAlignCenter));
-
- AWRTTextComponent *rewardCountTextComp = [[AWRTTextComponent alloc] init]
- .AWText([NSString stringWithFormat:@"*%ld", (long)rewardModel.rewardNum])
- .AWFont(Font(12))
- .AWColor(Color_TextFont);
- [richText addComponent:rewardCountTextComp];
- }
- }else {
- AWRTTextComponent *rewardCountTextComp = [[AWRTTextComponent alloc] init]
- .AWText(@"无")
- .AWFont(Font(12))
- .AWColor(Color_TextFont);
- [richText addComponent:rewardCountTextComp];
- }
-
- self.rewardLabel = [richText createRichTextLabel];
- self.rewardLabel.backgroundColor = Color_Clear;
- [self.infoView addSubview:self.rewardLabel];
- [self.rewardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.giftImageView.mas_right).offset(8.0f);
- make.top.equalTo(self.giftNameLabel.mas_bottom).offset(2.0f);
- make.height.equalTo(@20.0f);
- make.right.equalTo(self.wishSwitch.mas_left).offset(-10.0f);
- }];
- }
- - (void)setIndexPath:(NSIndexPath *)indexPath rowsInSection:(NSInteger)rows {
- self.indexPath = indexPath;
- }
- - (void)wishSwitchAction:(UISwitch *)sender {
- if (self.delegate && [self.delegate respondsToSelector:@selector(wishCenterGiftSwitchValueChangeAtIndexPath:)]) {
- sender.enabled = NO;
- [self.delegate wishCenterGiftSwitchValueChangeAtIndexPath:self.indexPath];
- }
- }
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
-
- [self setupUI];
- }
- return self;
- }
- - (void)setupUI {
-
- self.backgroundColor = self.contentView.backgroundColor = Color_Clear;
-
- [self.contentView addSubview:self.infoView];
- [self.infoView addSubview:self.giftImageView];
- [self.infoView addSubview:self.giftNameLabel];
- [self.infoView addSubview:self.wishSwitch];
- [self.infoView addSubview:self.completionTimeBeforeLabel];
- [self.infoView addSubview:self.completionTimeAfterLabel];
-
- [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.equalTo(self.contentView).offset(16.0f);
- make.right.equalTo(self.contentView).offset(-16.0f);
- make.bottom.equalTo(self.contentView);
- }];
- [self.giftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.infoView).offset(16.0f);
- make.centerY.equalTo(self.infoView);
- make.width.height.equalTo(@56.0f);
- }];
- [self.giftNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.giftImageView.mas_right).offset(8.0f);
- make.top.equalTo(self.infoView).offset(17.0f);
- make.height.equalTo(@20.0f);
- make.right.equalTo(self.wishSwitch.mas_left).offset(-10.0f);
- }];
- [self.wishSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.infoView).offset(-16.0f);
- make.centerY.equalTo(self.infoView);
- make.width.equalTo(@50.0f);
- make.height.equalTo(@30.0f);
- }];
- [self.completionTimeBeforeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.infoView).offset(-16.0f);
- make.top.equalTo(self.infoView).offset(21.0f);
- make.height.equalTo(@17.0f);
- }];
- [self.completionTimeAfterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.infoView).offset(-16.0f);
- make.top.equalTo(self.completionTimeBeforeLabel.mas_bottom).offset(4.0f);
- make.height.equalTo(@17.0f);
- }];
-
- [self.giftImageView addViewBorder:Color_Clear redian:56/2];
- [self.infoView addViewBorder:Color_Clear redian:8];
- }
- #pragma mark - Lazy
- - (UIView *)infoView {
- if (_infoView == nil) {
- _infoView = [UIView new];
- _infoView.backgroundColor = Color_White;
- }
- return _infoView;
- }
- - (UIImageView *)giftImageView {
- if (_giftImageView == nil) {
- _giftImageView = [UIImageView new];
- _giftImageView.image = placeholderImage();
- _giftImageView.clipsToBounds = YES;
- _giftImageView.contentMode = UIViewContentModeScaleAspectFill;
- }
- return _giftImageView;
- }
- - (UILabel *)giftNameLabel {
- if (_giftNameLabel == nil) {
- _giftNameLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font_B(14)];
- }
- return _giftNameLabel;
- }
- - (UISwitch *)wishSwitch {
- if (_wishSwitch == nil) {
- _wishSwitch = [[UISwitch alloc] init];
- _wishSwitch.onTintColor = Color_TextRed;
- _wishSwitch.thumbTintColor = Color_Background;
- [_wishSwitch addTarget:self action:@selector(wishSwitchAction:) forControlEvents:UIControlEventValueChanged];
- }
- return _wishSwitch;
- }
- - (UILabel *)completionTimeBeforeLabel {
- if (_completionTimeBeforeLabel == nil) {
- _completionTimeBeforeLabel = [UILabel createLabelTextColor:Color_TextGray fount:Font(12)];
- _completionTimeBeforeLabel.textAlignment = NSTextAlignmentRight;
- }
- return _completionTimeBeforeLabel;
- }
- - (UILabel *)completionTimeAfterLabel {
- if (_completionTimeAfterLabel == nil) {
- _completionTimeAfterLabel = [UILabel createLabelTextColor:Color_TextGray fount:Font(12)];
- _completionTimeAfterLabel.textAlignment = NSTextAlignmentRight;
- }
- return _completionTimeAfterLabel;
- }
- - (void)awakeFromNib {
- [super awakeFromNib];
- // Initialization code
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- @end
|