RQCommonCollectionViewCell.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // RQCommonCollectionViewCell.m
  3. // YueXueChe
  4. //
  5. // Created by 张嵘 on 2018/12/19.
  6. // Copyright © 2018 lee. All rights reserved.
  7. //
  8. #import "RQCommonCollectionViewCell.h"
  9. #import "RQCommonCollectionItemViewModel.h"
  10. @interface RQCommonCollectionViewCell ()
  11. /// viewModel
  12. @property (nonatomic, readwrite, strong) RQCommonCollectionItemViewModel *viewModel;
  13. @property (strong, readwrite, nonatomic) UIImageView *iconImageView;
  14. @property (strong, readwrite, nonatomic) UILabel *titleLabel;
  15. @end
  16. @implementation RQCommonCollectionViewCell
  17. + (instancetype)cellWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath {
  18. NSString *ID = [NSString stringWithFormat:@"RQCommonCollectionViewCell%ld",(long)indexPath.section];
  19. [collectionView registerClass:[RQCommonCollectionViewCell class] forCellWithReuseIdentifier:ID];
  20. RQCommonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
  21. cell.backgroundColor = RQ_MAIN_BACKGROUNDCOLOR;
  22. if (!cell) {
  23. cell = [[self alloc] init];
  24. cell.iconImageView.hidden = YES;
  25. cell.titleLabel.hidden = YES;
  26. }
  27. return cell;
  28. }
  29. - (void)bindViewModel:(RQCommonCollectionItemViewModel *)viewModel {
  30. self.viewModel = viewModel;
  31. BOOL isURL = [NSString rq_isValidURL:viewModel.icon];
  32. if (isURL) {
  33. [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) {
  34. if(image) {
  35. image = [image qmui_imageResizedInLimitedSize:CGSizeMake(RQ_FIT_HORIZONTAL(25.f), RQ_FIT_HORIZONTAL(25.f)) resizingMode:QMUIImageResizingModeScaleAspectFill];
  36. self.iconImageView.image = image;
  37. }
  38. }];
  39. // [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:viewModel.icon] placeholderImage:RQWebImagePlaceholder()];
  40. } else {
  41. self.iconImageView.image = [UIImage imageNamed:viewModel.icon];
  42. }
  43. self.titleLabel.text = viewModel.title;
  44. self.iconImageView.hidden = RQStringIsEmpty(viewModel.icon);
  45. self.titleLabel.hidden = RQStringIsEmpty(viewModel.title);
  46. }
  47. - (void)setIndexPath:(NSIndexPath *)indexPath rowsInSection:(NSInteger)rows {
  48. }
  49. - (instancetype)init {
  50. if (self = [super init]) {
  51. // 初始化
  52. [self _setup];
  53. // 创建自控制器
  54. [self _setupSubViews];
  55. // 布局子控件
  56. [self _makeSubViewsConstraints];
  57. }
  58. return self;
  59. }
  60. #pragma mark - 初始化
  61. - (void)_setup{
  62. self.contentView.backgroundColor = RQ_MAIN_BACKGROUNDCOLOR;
  63. }
  64. #pragma mark - 创建自控制器
  65. - (void)_setupSubViews {
  66. [self.contentView addSubview:self.iconImageView];
  67. [self.contentView addSubview:self.titleLabel];
  68. }
  69. #pragma mark - 布局子控件
  70. - (void)_makeSubViewsConstraints{
  71. }
  72. #pragma mark - 布局
  73. - (void)layoutSubviews{
  74. [super layoutSubviews];
  75. @weakify(self)
  76. [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  77. @strongify(self)
  78. make.centerX.mas_equalTo(self.contentView);
  79. make.centerY.mas_equalTo(self.contentView).mas_offset(-15.f);
  80. make.size.mas_offset(CGSizeMake(RQ_FIT_HORIZONTAL(25.f), RQ_FIT_HORIZONTAL(25.f)));
  81. }];
  82. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  83. @strongify(self)
  84. make.centerX.mas_equalTo(self.contentView);
  85. make.top.mas_equalTo(self.iconImageView.mas_bottom).mas_offset(8.f);
  86. make.height.mas_offset(18.f);
  87. }];
  88. }
  89. #pragma mark - LazyLoad
  90. - (UIImageView *)iconImageView {
  91. if (!_iconImageView) {
  92. _iconImageView = [[UIImageView alloc] init];
  93. _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
  94. [self.contentView addSubview:self.iconImageView];
  95. }
  96. return _iconImageView;
  97. }
  98. - (UILabel *)titleLabel {
  99. if (!_titleLabel) {
  100. _titleLabel = [[UILabel alloc] init];
  101. _titleLabel.textColor = RQ_MAIN_TEXT_COLOR_1;
  102. _titleLabel.font = RQRegularFont_15;
  103. _titleLabel.textAlignment = NSTextAlignmentCenter;
  104. [self.contentView addSubview:self.titleLabel];
  105. }
  106. return _titleLabel;
  107. }
  108. - (void)setSelected:(BOOL)selected {
  109. [super setSelected:selected];
  110. if (selected) {
  111. self.contentView.layer.borderColor = RQ_MAIN_COLOR.CGColor;
  112. self.titleLabel.textColor = RQ_MAIN_COLOR;
  113. }else {
  114. self.contentView.layer.borderColor = UIColor.lightGrayColor.CGColor;
  115. self.titleLabel.textColor = RQ_MAIN_TEXT_COLOR_1;
  116. }
  117. }
  118. @end