MOGradeCollectionCell.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // MOGradeCollectionCell.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2023/12/27.
  6. //
  7. #import "MOGradeCollectionCell.h"
  8. @interface MOGradeCollectionCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
  9. @property (nonatomic, strong) UICollectionView *collectionView;
  10. @end
  11. @implementation MOGradeCollectionCell
  12. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  13. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  14. if (self != nil) {
  15. self.selectionStyle = UITableViewCellSelectionStyleNone;
  16. self.backgroundColor = [UIColor clearColor];
  17. [self setupUI];
  18. }
  19. return self;
  20. }
  21. - (void)setupUI{
  22. [self.contentView addSubview:self.collectionView];
  23. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  24. make.edges.equalTo(self.contentView);
  25. }];
  26. }
  27. - (void)awakeFromNib {
  28. [super awakeFromNib];
  29. // Initialization code
  30. }
  31. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  32. [super setSelected:selected animated:animated];
  33. // Configure the view for the selected state
  34. }
  35. - (void)setDataArr:(NSArray *)dataArr{
  36. _dataArr = dataArr;
  37. [self.collectionView reloadData];
  38. }
  39. #pragma mark --UICollectionViewDatasource & delegate
  40. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  41. return 1;
  42. }
  43. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  44. return self.dataArr.count;
  45. }
  46. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  47. MOPrivilegesItemInfo *model = self.dataArr[indexPath.row];
  48. MOGradeInfoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MOGradeInfoCell_ID forIndexPath:indexPath];
  49. cell.level = self.level;
  50. cell.cellModel = model;
  51. return cell;
  52. }
  53. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  54. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  55. MOPrivilegesItemInfo *model = self.dataArr[indexPath.row];
  56. self.cellClickBlock ? self.cellClickBlock(model) : nil;
  57. }
  58. - (UICollectionView *)collectionView{
  59. if (!_collectionView)
  60. {
  61. CGFloat collectionWidth = SCREENWIDTH - 4.0;
  62. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  63. layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
  64. layout.minimumLineSpacing = 5.0;
  65. layout.minimumInteritemSpacing = 0.0;
  66. layout.itemSize = CGSizeMake(collectionWidth / 4.0 ,120.0);
  67. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, collectionWidth, collectionWidth / 2) collectionViewLayout:layout];
  68. [self.collectionView registerNib:[UINib nibWithNibName:@"MOGradeInfoCell" bundle:nil] forCellWithReuseIdentifier:MOGradeInfoCell_ID];
  69. _collectionView.dataSource = self;
  70. _collectionView.delegate = self;
  71. _collectionView.showsVerticalScrollIndicator = NO;
  72. _collectionView.backgroundColor = [UIColor clearColor];
  73. _collectionView.scrollEnabled = NO;
  74. }
  75. return _collectionView;
  76. }
  77. @end