// // KDPSupplyCommentViewCell.m // KuDianProject // // Created by admin on 2019/7/4. // Copyright © 2019 KDP. All rights reserved. // #import "KDPSupplyCommentViewCell.h" #import "KDPClassModel.h" @interface KDPSupplyCommentViewCell () @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation KDPSupplyCommentViewCell - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.contentView.layer.cornerRadius = 8; self.contentView.layer.masksToBounds = YES; self.contentView.backgroundColor = [UIColor whiteColor]; [self setUpSubViews]; } return self; } - (void)setUpSubViews{ [self.contentView addSubview:self.collectionView]; } - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH-20)/4, 90); flowLayout.minimumLineSpacing = 0; flowLayout.minimumInteritemSpacing = 0; _collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:flowLayout]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.scrollEnabled = NO; _collectionView.backgroundColor = [UIColor whiteColor]; [_collectionView registerClass:[KDPSupplyCommentContentCell class] forCellWithReuseIdentifier:NSStringFromClass([KDPSupplyCommentContentCell class])]; } return _collectionView; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ KDPSupplyCommentContentCell *contentCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KDPSupplyCommentContentCell class]) forIndexPath:indexPath]; [contentCell configCellWithModel:self.dataArray[indexPath.row] indexPath:indexPath]; return contentCell; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataArray.count >= 8 ? 8 : self.dataArray.count; } - (void)setDataArray:(NSArray *)dataArray{ _dataArray = dataArray; [self.collectionView reloadData]; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if ([self.delegate respondsToSelector:@selector(commentCollectionView:didSelectIndex:)]) { [self.delegate commentCollectionView:collectionView didSelectIndex:indexPath.row]; } } @end @implementation KDPSupplyCommentContentCell { UIImageView *_commentImageView; UILabel *_commentLabel; } - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self setUpSubViews]; [self setSubViewsConstraints]; } return self; } - (void)setUpSubViews{ _commentImageView = [[UIImageView alloc] init]; [self.contentView addSubview:_commentImageView]; _commentLabel = [[UILabel alloc] init]; _commentLabel.font = FONT_SYS(13); _commentLabel.textColor = [UIColor colorWithRGB:0x666666]; _commentLabel.textAlignment = NSTextAlignmentCenter; [self.contentView addSubview:_commentLabel]; } - (void)setSubViewsConstraints{ [_commentImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView.mas_top).offset(30); make.size.equalTo(CGSizeMake(20, 35)); make.centerX.equalTo(self.contentView); }]; [_commentLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self->_commentImageView.mas_bottom).offset(3); make.left.equalTo(self.contentView.mas_left).offset(10); make.right.equalTo(self.contentView.mas_right).offset(-10); make.centerX.equalTo(self.contentView); }]; } - (void)configCellWithModel:(id)model indexPath:(NSIndexPath *)indexpath{ KDPClassModel *goodModel = (KDPClassModel *)model; [_commentImageView sd_setImageWithURL:[NSURL URLWithString:goodModel.icon]placeholderImage:[UIImage imageNamed:placholderImg]]; _commentLabel.text = goodModel.name; } @end