RQCommonCollectionViewCell.m 3.2 KB

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