MORoomBackgroundSetView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // MORoomBackgroundSetView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2024/8/13.
  6. //
  7. #define kCollectionViewLeftAndRight 15.0
  8. #import "MORoomBackgroundSetView.h"
  9. @interface MORoomBackgroundSetView ()<UICollectionViewDelegate,UICollectionViewDataSource>
  10. /** 主界面 */
  11. @property (nonatomic, strong) UICollectionView *collectionView;
  12. @end
  13. @implementation MORoomBackgroundSetView
  14. - (instancetype)init{
  15. self = [super init];
  16. if (self)
  17. {
  18. [self setupUI];
  19. }
  20. return self;
  21. }
  22. - (instancetype)initWithFrame:(CGRect)frame
  23. {
  24. self = [super initWithFrame:frame];
  25. if (self)
  26. {
  27. [self setupUI];
  28. }
  29. return self;
  30. }
  31. - (void)setupUI{
  32. [self addSubview:self.collectionView];
  33. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.left.equalTo(self).offset(15.0);
  35. make.width.equalTo(self).offset(-15.0);
  36. make.top.bottom.equalTo(self);
  37. }];
  38. }
  39. #pragma mark - JXCategoryListContentViewDelegate
  40. - (void)listWillAppear{
  41. }
  42. - (UIView *)listView{
  43. return self;
  44. }
  45. - (void)setDataArr:(NSArray *)dataArr{
  46. _dataArr = dataArr;
  47. if(dataArr.count != 0){
  48. [self.collectionView reloadData];
  49. }
  50. }
  51. #pragma mark UICollectionViewDelegate,UICollectionViewDataSource
  52. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  53. return 1;
  54. }
  55. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  56. return self.dataArr.count;
  57. }
  58. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  59. MOMicroStyleModel *model = self.dataArr[indexPath.row];
  60. MORoomBackgroundCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MORoomBackgroundCell_ID forIndexPath:indexPath];
  61. cell.cellModel = model;
  62. return cell;
  63. }
  64. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  65. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  66. for (MOMicroStyleModel *model in self.dataArr) {
  67. model.isChoose = NO;
  68. }
  69. MOMicroStyleModel *model = self.dataArr[indexPath.row];
  70. model.isChoose = YES;
  71. [self.collectionView reloadData];
  72. self.didSelectCellBlock ? self.didSelectCellBlock(model) : nil;
  73. }
  74. #pragma mark Lazy
  75. - (UICollectionView *)collectionView
  76. {
  77. if (!_collectionView)
  78. {
  79. CGFloat collectionWidth = SCREENWIDTH - kCollectionViewLeftAndRight * 2;
  80. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  81. layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
  82. layout.minimumLineSpacing = 16.0;
  83. layout.minimumInteritemSpacing = 15.0;
  84. CGFloat width = (collectionWidth - 16.0 * 3.0) / 4.0;
  85. CGFloat height = width * 94.0 / 74.0;
  86. layout.itemSize = CGSizeMake(width ,height);
  87. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, collectionWidth, collectionWidth) collectionViewLayout:layout];
  88. [_collectionView registerNib:[UINib nibWithNibName:@"MORoomBackgroundCell" bundle:nil] forCellWithReuseIdentifier:MORoomBackgroundCell_ID];
  89. _collectionView.dataSource = self;
  90. _collectionView.delegate = self;
  91. _collectionView.showsVerticalScrollIndicator = NO;
  92. _collectionView.layer.cornerRadius = 12.0;
  93. _collectionView.layer.masksToBounds = YES;
  94. _collectionView.backgroundColor = [UIColor clearColor];
  95. _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
  96. }
  97. return _collectionView;
  98. }
  99. @end