MOLanguageItemView.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // MOLanguageItemView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2023/10/9.
  6. //
  7. #import "MOLanguageItemView.h"
  8. #import "MOLanguageItemCell.h"
  9. @implementation MOLanItemModel
  10. @end
  11. @interface MOLanguageItemView ()<UICollectionViewDelegate,UICollectionViewDataSource>
  12. @property (nonatomic, strong) UICollectionView *collectionView;///<主视图
  13. @end
  14. @implementation MOLanguageItemView
  15. - (instancetype)initWithFrame:(CGRect)frame{
  16. if (self = [super initWithFrame:frame])
  17. {
  18. self.backgroundColor = [UIColor clearColor];
  19. self.layer.cornerRadius = 12.0;
  20. self.layer.masksToBounds = YES;
  21. [self setupUI];
  22. }
  23. return self;
  24. }
  25. - (void)awakeFromNib{
  26. [super awakeFromNib];
  27. self.backgroundColor = [UIColor clearColor];
  28. self.layer.cornerRadius = 12.0;
  29. self.layer.masksToBounds = YES;
  30. [self setupUI];
  31. }
  32. - (void)layoutSubviews{
  33. [super layoutSubviews];
  34. }
  35. - (void)setupUI
  36. {
  37. [self addSubview:self.titleLab];
  38. [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.left.mas_equalTo(26.0);
  40. make.top.equalTo(self).offset(14.0);
  41. make.right.mas_equalTo(-9.0);
  42. make.height.mas_greaterThanOrEqualTo(18.0);
  43. }];
  44. [self addSubview:self.collectionView];
  45. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make){
  46. make.top.equalTo(self.titleLab.mas_bottom).offset(15);
  47. make.left.equalTo(self).offset(24.0);
  48. make.right.equalTo(self).offset(-24.0);
  49. make.bottom.equalTo(self);
  50. }];
  51. }
  52. - (void)setDataArr:(NSMutableArray *)dataArr
  53. {
  54. _dataArr = dataArr;
  55. [self.collectionView reloadData];
  56. }
  57. #pragma mark --UICollectionViewDatasource & delegate
  58. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  59. return 1;
  60. }
  61. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  62. return self.dataArr.count;
  63. }
  64. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  65. MOCountryList *itemModel = self.dataArr[indexPath.row];
  66. MOLanguageItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MOLanguageItemCell_ID forIndexPath:indexPath];
  67. cell.model = itemModel;
  68. return cell;
  69. }
  70. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  71. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  72. MOCountryList *itemModel = self.dataArr[indexPath.row];
  73. for (MOCountryList *model in self.dataArr) {
  74. model.isChoose = NO;
  75. }
  76. itemModel.isChoose = YES;
  77. [self.collectionView reloadData];
  78. self.selectBlock ? self.selectBlock(itemModel) : nil;
  79. }
  80. #pragma mark - Lazy
  81. - (UILabel *)titleLab
  82. {
  83. if (!_titleLab)
  84. {
  85. _titleLab = [[UILabel alloc] init];
  86. _titleLab.text = @"";
  87. _titleLab.font = [MOTextTools getTheFontWithSize:20.0 AndFontName:kNormalContentFontStr];
  88. _titleLab.textColor = [MOTools colorWithHexString:@"#282828" alpha:1.0];
  89. _titleLab.textAlignment = NSTextAlignmentLeft;
  90. _titleLab.numberOfLines = 0;
  91. }
  92. return _titleLab;
  93. }
  94. - (UICollectionView *)collectionView
  95. {
  96. if (!_collectionView)
  97. {
  98. UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
  99. CGFloat width = (SCREENWIDTH - 39.0 * 2.0 - 10) / 2.0;
  100. CGFloat height = 30.0;
  101. flow.itemSize = CGSizeMake(width, height);
  102. flow.minimumLineSpacing = 10;//行间距
  103. flow.minimumInteritemSpacing = 10;//列间距
  104. flow.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
  105. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, (SCREENWIDTH - 12.0 * 2), 100) collectionViewLayout:flow];
  106. _collectionView.allowsMultipleSelection = YES;
  107. _collectionView.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF"];
  108. [_collectionView registerClass:[MOLanguageItemCell class] forCellWithReuseIdentifier:MOLanguageItemCell_ID];
  109. _collectionView.layer.masksToBounds = YES;
  110. _collectionView.layer.cornerRadius = 13;
  111. _collectionView.dataSource = self;
  112. _collectionView.delegate = self;
  113. }
  114. return _collectionView;
  115. }
  116. @end