// // 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]; } return cell; } - (void)bindViewModel:(RQCommonCollectionItemViewModel *)viewModel { self.viewModel = viewModel; self.iconImageView.image = [UIImage imageNamed:viewModel.icon]; self.titleLabel.text = 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]; [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.contentView); make.centerY.mas_equalTo(self.contentView).mas_offset(-8.f); make.size.mas_offset(CGSizeMake(25.f, 25.f)); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.contentView); make.top.mas_equalTo(self.iconImageView.mas_bottom).mas_offset(8.f); make.height.mas_offset(10.f); }]; } #pragma mark - LazyLoad - (UIImageView *)iconImageView { if (!_iconImageView) { _iconImageView = [[UIImageView alloc] init]; [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_12; _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