123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- //
- // 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
|