ASHomeTipCell.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // ASHomeTipCell.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/6/6.
  6. //
  7. #import "ASHomeTipCell.h"
  8. #import "ASHomeTipCollectCell.h"
  9. @interface ASHomeTipCell () <UICollectionViewDelegate,UICollectionViewDataSource>
  10. @property (nonatomic, strong) UICollectionView *collV;
  11. @property (nonatomic, strong) NSArray <NSString *>*titleArr;
  12. @property (nonatomic, strong) NSArray <NSString *>*imgArr;
  13. @end
  14. @implementation ASHomeTipCell
  15. - (void)setData {
  16. self.titleArr = [NSArray arrayWithObjects:@"USA UK CAN\nFREE SHIPPING",@"INTERNATIONAL\nEXPRESS SHIPPING",@"SECURE PAYMENTS\nCREDIT PAY IN 4",@"30 DAYS FREE\nRETURN/EXCHANGE", nil];
  17. self.imgArr = @[@"trade_tip",@"air_tip",@"pay_tip",@"box_tip"];
  18. [self.collV reloadData];
  19. }
  20. - (void)awakeFromNib {
  21. [super awakeFromNib];
  22. // Initialization code
  23. }
  24. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  25. [super setSelected:selected animated:animated];
  26. // Configure the view for the selected state
  27. }
  28. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  29. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  30. if (self) {
  31. self.selectionStyle = UITableViewCellSelectionStyleNone;
  32. [self.contentView addSubview:self.collV];
  33. CGFloat margin = 10;
  34. CGFloat w = (KScreenWidth- 3*margin-1)/2;
  35. CGFloat h = w/172*55;
  36. CGFloat colH = h*2 + margin+2;
  37. [self.collV mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.top.equalTo(self.contentView).offset(25);
  39. make.leading.trailing.equalTo(self.contentView);
  40. make.height.equalTo(@(colH));
  41. make.bottom.equalTo(self.contentView).offset(-25);
  42. }];
  43. [self setData];
  44. }
  45. return self;
  46. }
  47. - (UICollectionView *)collV {
  48. if (!_collV) {
  49. UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
  50. CGFloat margin = 10;
  51. CGFloat w = (KScreenWidth- 3*margin-1)/2;
  52. CGFloat h = w/172*55;
  53. CGFloat colH = h*2 + margin+2;
  54. layout.itemSize = CGSizeMake(w, h) ;
  55. layout.sectionInset = UIEdgeInsetsMake(0, margin, 0, margin);
  56. layout.minimumLineSpacing = margin;
  57. layout.minimumInteritemSpacing = margin;
  58. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  59. UICollectionView *col = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, colH) collectionViewLayout:layout];
  60. [col registerClass:[ASHomeTipCollectCell class] forCellWithReuseIdentifier:@"ASHomeTipCollectCell"];
  61. col.showsHorizontalScrollIndicator = false;
  62. col.delegate = self;
  63. col.dataSource = self;
  64. _collV = col;
  65. }
  66. return _collV;
  67. }
  68. // MARK: - coll delegate datasource
  69. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  70. return self.titleArr.count;
  71. }
  72. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  73. ASHomeTipCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ASHomeTipCollectCell" forIndexPath:indexPath];
  74. if (self.titleArr.count <= indexPath.row) {
  75. return cell;
  76. }
  77. NSString *titleStr = self.titleArr[indexPath.row];
  78. NSString *imgName = self.imgArr[indexPath.row];
  79. cell.imgV.image = [UIImage imageNamed:imgName];
  80. cell.titleLb.text = titleStr;
  81. return cell;
  82. }
  83. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  84. }
  85. @end