Files
mier_ios/SweetParty/主类/Homepage/View/SPHomepageGiftView.m
2025-08-11 10:43:19 +08:00

88 lines
2.8 KiB
Objective-C
Executable File

//
// SPHomepageGiftView.m
// SweetParty
//
// Created by bj_szd on 2022/6/2.
//
#import "SPHomepageGiftView.h"
#import "SPHomepageGiftCell.h"
@interface SPHomepageGiftView () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) NSArray *dataArray;
@end
@implementation SPHomepageGiftView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createUI];
}
return self;
}
- (void)setModel:(SPHomepageModel *)model {
_model = model;
self.dataArray = model.receive_gift_list;
[self.collectionView reloadData];
NSInteger lines = model.receive_gift_list.count%3 == 0 ? model.receive_gift_list.count/3 : model.receive_gift_list.count/3+1;
[self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(125*lines+20);
}];
}
- (void)createUI {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
CGFloat itemW = (ScreenWidth-14*2-10*2)/3;
layout.itemSize = CGSizeMake(itemW, 125);
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 10;
layout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.backgroundColor = kClearColor;
// _collectionView.layer.cornerRadius = 15;
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView registerNib:[UINib nibWithNibName:@"SPHomepageGiftCell" bundle:nil] forCellWithReuseIdentifier:@"SPHomepageGiftCell"];
[self addSubview:_collectionView];
[_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self);
make.left.equalTo(self).offset(14);
make.right.equalTo(self).offset(-14);
make.height.mas_equalTo(0);
}];
}
#pragma mark -------- collectionView代理方法 ------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
SPHomepageGiftCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SPHomepageGiftCell" forIndexPath:indexPath];
SPHomepageGiftModel *model = self.dataArray[indexPath.row];
[cell.imgV sd_setImageWithURL:[NSURL URLWithString:model.base_image]];
cell.nameLab.text = model.gift_name;
// [cell.numBtn setTitle:[NSString stringWithFormat:@"x%@", model.gift_total_sum] forState:UIControlStateNormal];
return cell;
}
@end