RQCommonCollectionViewCell.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "RQCommonCollecttionItemViewModel.h"
  10. @interface RQCommonCollectionViewCell ()
  11. /// viewModel
  12. @property (nonatomic, readwrite, strong) RQCommonCollecttionItemViewModel *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. }
  25. return cell;
  26. }
  27. - (void)bindViewModel:(RQCommonCollecttionItemViewModel *)viewModel {
  28. self.viewModel = viewModel;
  29. self.iconImageView.hidden = RQStringIsEmpty(viewModel.icon);
  30. self.titleLabel.hidden = RQStringIsEmpty(viewModel.title);
  31. self.iconImageView.image = [UIImage imageNamed:viewModel.icon];
  32. self.titleLabel.text = viewModel.title;
  33. if (RQStringIsEmpty(viewModel.icon)) {
  34. _iconImageView.hidden = YES;
  35. _titleLabel.textAlignment = NSTextAlignmentLeft;
  36. _titleLabel.numberOfLines = 0;
  37. [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  38. make.top.left.bottom.right.mas_equalTo(self.contentView);
  39. }];
  40. }
  41. }
  42. - (void)setIndexPath:(NSIndexPath *)indexPath rowsInSection:(NSInteger)rows {
  43. }
  44. - (instancetype)init {
  45. if (self = [super init]) {
  46. // 初始化
  47. [self _setup];
  48. // 创建自控制器
  49. [self _setupSubViews];
  50. // 布局子控件
  51. [self _makeSubViewsConstraints];
  52. }
  53. return self;
  54. }
  55. #pragma mark - 初始化
  56. - (void)_setup{
  57. self.contentView.backgroundColor = RQ_MAIN_BACKGROUNDCOLOR;
  58. }
  59. #pragma mark - 创建自控制器
  60. - (void)_setupSubViews {
  61. [self.contentView addSubview:self.iconImageView];
  62. [self.contentView addSubview:self.titleLabel];
  63. }
  64. #pragma mark - 布局子控件
  65. - (void)_makeSubViewsConstraints{
  66. }
  67. #pragma mark - 布局
  68. - (void)layoutSubviews{
  69. [super layoutSubviews];
  70. [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.centerX.mas_equalTo(self.contentView);
  72. make.centerY.mas_equalTo(self.contentView).mas_offset(-8.f);
  73. make.size.mas_offset(CGSizeMake(25.f, 25.f));
  74. }];
  75. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.left.right.mas_equalTo(self.contentView);
  77. // make.top.mas_equalTo(self.iconImageView.mas_bottom).mas_offset(8.f);
  78. make.bottom.mas_equalTo(self.contentView).mas_offset(-8.f);
  79. }];
  80. }
  81. #pragma mark - LazyLoad
  82. - (UIImageView *)iconImageView {
  83. if (!_iconImageView) {
  84. _iconImageView = [[UIImageView alloc] init];
  85. _iconImageView.hidden = YES;
  86. [self.contentView addSubview:self.iconImageView];
  87. }
  88. return _iconImageView;
  89. }
  90. - (UILabel *)titleLabel {
  91. if (!_titleLabel) {
  92. _titleLabel = [[UILabel alloc] init];
  93. _titleLabel.textColor = RQ_MAIN_TEXT_COLOR_1;
  94. _titleLabel.font = RQRegularFont_17;
  95. _titleLabel.textAlignment = NSTextAlignmentCenter;
  96. _titleLabel.hidden = YES;
  97. [self.contentView addSubview:self.titleLabel];
  98. }
  99. return _titleLabel;
  100. }
  101. - (void)setSelected:(BOOL)selected {
  102. [super setSelected:selected];
  103. if (selected) {
  104. self.contentView.layer.borderColor = RQ_MAIN_COLOR.CGColor;
  105. self.titleLabel.textColor = RQ_MAIN_COLOR;
  106. }else {
  107. self.contentView.layer.borderColor = UIColor.lightGrayColor.CGColor;
  108. self.titleLabel.textColor = RQ_MAIN_TEXT_COLOR_1;
  109. }
  110. }
  111. @end