ASCategaryListCell.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // ASCategaryListCell.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/6/6.
  6. //
  7. #import "ASCategaryListCell.h"
  8. #import "ASCategaryCollectCell.h"
  9. @interface ASCategaryListCell () <UICollectionViewDelegate,UICollectionViewDataSource>
  10. @property (nonatomic, strong) UICollectionView *collV;
  11. @end
  12. @implementation ASCategaryListCell
  13. - (void)setArr:(NSArray<ASHomeCategoryModel *> *)arr {
  14. _arr = arr;
  15. [self.collV reloadData];
  16. }
  17. - (void)awakeFromNib {
  18. [super awakeFromNib];
  19. // Initialization code
  20. }
  21. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  22. [super setSelected:selected animated:animated];
  23. // Configure the view for the selected state
  24. }
  25. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  26. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  27. if (self) {
  28. self.selectionStyle = UITableViewCellSelectionStyleNone;
  29. self.contentView.backgroundColor = UIColor.whiteColor;
  30. [self.contentView addSubview:self.collV];
  31. [self.collV mas_makeConstraints:^(MASConstraintMaker *make) {
  32. make.top.equalTo(self.contentView).offset(30);
  33. make.leading.trailing.equalTo(self.contentView);
  34. make.height.equalTo(@112);
  35. make.bottom.equalTo(self.contentView).offset(-25);
  36. }];
  37. }
  38. return self;
  39. }
  40. - (UICollectionView *)collV {
  41. if (!_collV) {
  42. UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
  43. layout.itemSize = CGSizeMake(112, 112);
  44. layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
  45. layout.minimumLineSpacing = 5;
  46. layout.minimumInteritemSpacing = 5;
  47. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  48. UICollectionView *col = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 112) collectionViewLayout:layout];
  49. [col registerClass:[ASCategaryCollectCell class] forCellWithReuseIdentifier:@"ASCategaryCollectCell"];
  50. col.showsHorizontalScrollIndicator = false;
  51. col.delegate = self;
  52. col.dataSource = self;
  53. col.backgroundColor = UIColor.whiteColor;
  54. _collV = col;
  55. }
  56. return _collV;
  57. }
  58. // MARK: - coll delegate datasource
  59. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  60. return _arr.count;
  61. }
  62. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  63. ASCategaryCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASCategaryCollectCell" forIndexPath:indexPath];
  64. if (self.arr.count <= indexPath.row) {
  65. return cell;
  66. }
  67. ASHomeCategoryModel *m = self.arr[indexPath.row];
  68. [cell.imgV sd_setImageWithURL:[NSURL URLWithString:m.imgUrl]];
  69. cell.titleLb.text = m.title;
  70. return cell;
  71. }
  72. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  73. if (self.arr.count <= indexPath.row) {
  74. return ;
  75. }
  76. ASHomeCategoryModel *m = self.arr[indexPath.row];
  77. if (self.selectCategory) {
  78. self.selectCategory(m, indexPath);
  79. }
  80. }
  81. @end