// // RQCommonCollectionViewCell.m // YueXueChe // // Created by 张嵘 on 2018/12/19. // Copyright © 2018 lee. All rights reserved. // #import "RQCommonCollectionViewCell.h" #import "RQCommonCollectionItemViewModel.h" @interface RQCommonCollectionViewCell () /// viewModel @property (nonatomic, readwrite, strong) RQCommonCollectionItemViewModel *viewModel; @property (strong, readwrite, nonatomic) UIImageView *iconImageView; @property (strong, readwrite, nonatomic) UILabel *titleLabel; @end @implementation RQCommonCollectionViewCell + (instancetype)cellWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath { NSString *ID = [NSString stringWithFormat:@"RQCommonCollectionViewCell%ld",(long)indexPath.section]; [collectionView registerClass:[RQCommonCollectionViewCell class] forCellWithReuseIdentifier:ID]; RQCommonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; cell.backgroundColor = RQ_MAIN_BACKGROUNDCOLOR; if (!cell) { cell = [[self alloc] init]; cell.iconImageView.hidden = YES; cell.titleLabel.hidden = YES; } return cell; } - (void)bindViewModel:(RQCommonCollectionItemViewModel *)viewModel { self.viewModel = viewModel; BOOL isURL = [NSString rq_isValidURL:viewModel.icon]; if (isURL) { [self.iconImageView yy_setImageWithURL:[NSURL URLWithString:viewModel.icon] placeholder:RQWebAvatarImagePlaceholder() options:RQWebImageOptionAutomatic completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) { if(image) { image = [image qmui_imageResizedInLimitedSize:CGSizeMake(RQ_FIT_HORIZONTAL(25.f), RQ_FIT_HORIZONTAL(25.f)) resizingMode:QMUIImageResizingModeScaleAspectFill]; self.iconImageView.image = image; } }]; // [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:viewModel.icon] placeholderImage:RQWebImagePlaceholder()]; } else { self.iconImageView.image = [UIImage imageNamed:viewModel.icon]; } self.titleLabel.text = viewModel.title; self.iconImageView.hidden = RQStringIsEmpty(viewModel.icon); self.titleLabel.hidden = RQStringIsEmpty(viewModel.title); } - (void)setIndexPath:(NSIndexPath *)indexPath rowsInSection:(NSInteger)rows { } - (instancetype)init { if (self = [super init]) { // 初始化 [self _setup]; // 创建自控制器 [self _setupSubViews]; // 布局子控件 [self _makeSubViewsConstraints]; } return self; } #pragma mark - 初始化 - (void)_setup{ self.contentView.backgroundColor = RQ_MAIN_BACKGROUNDCOLOR; } #pragma mark - 创建自控制器 - (void)_setupSubViews { [self.contentView addSubview:self.iconImageView]; [self.contentView addSubview:self.titleLabel]; } #pragma mark - 布局子控件 - (void)_makeSubViewsConstraints{ } #pragma mark - 布局 - (void)layoutSubviews{ [super layoutSubviews]; @weakify(self) [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) { @strongify(self) make.centerX.mas_equalTo(self.contentView); make.centerY.mas_equalTo(self.contentView).mas_offset(-15.f); make.size.mas_offset(CGSizeMake(RQ_FIT_HORIZONTAL(25.f), RQ_FIT_HORIZONTAL(25.f))); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { @strongify(self) make.centerX.mas_equalTo(self.contentView); make.top.mas_equalTo(self.iconImageView.mas_bottom).mas_offset(8.f); make.height.mas_offset(18.f); }]; } #pragma mark - LazyLoad - (UIImageView *)iconImageView { if (!_iconImageView) { _iconImageView = [[UIImageView alloc] init]; _iconImageView.contentMode = UIViewContentModeScaleAspectFill; [self.contentView addSubview:self.iconImageView]; } return _iconImageView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.textColor = RQ_MAIN_TEXT_COLOR_1; _titleLabel.font = RQRegularFont_15; _titleLabel.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:self.titleLabel]; } return _titleLabel; } - (void)setSelected:(BOOL)selected { [super setSelected:selected]; if (selected) { self.contentView.layer.borderColor = RQ_MAIN_COLOR.CGColor; self.titleLabel.textColor = RQ_MAIN_COLOR; }else { self.contentView.layer.borderColor = UIColor.lightGrayColor.CGColor; self.titleLabel.textColor = RQ_MAIN_TEXT_COLOR_1; } } @end