// // FKRecoUserRecoProductCell.m // FirstLink // // Created by ascii on 16/9/25. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKRecoUserRecoProductCell.h" #import "FKRecoUserCollectionViewCell.h" #import "FKHotProductItem.h" #import "FKRecommendViewModel.h" @interface FKRecoUserRecoProductCell () @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSArray *dataSource; @end @implementation FKRecoUserRecoProductCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.contentView.backgroundColor = [UIColor whiteColor]; [self addAllSubviews]; } return self; } - (void)fk_configWithViewModel:(FKRecommendViewModel *)viewModel indexPath:(NSIndexPath *)indexPath { if ([viewModel isKindOfClass:[FKRecommendViewModel class]]) { self.dataSource = viewModel.userRecoArea.dataArray; [self.collectionView reloadData]; } } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataSource.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { FKRecoUserCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FKRecoUserCollectionViewCell class]) forIndexPath:indexPath]; FKHotProductItem *item = [self itemAtIndex:indexPath.row]; if (item) { [cell.imageView setImageWithURL:item.picUrl cdnWidth:[FKRecoUserRecoProductCell imgLength:UISCREENWIDTH]]; cell.titleLabel.text = item.title; cell.priceLabel.attributedText = [item priceAttributedString]; } return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { FKHotProductItem *item = [self itemAtIndex:indexPath.row]; if (item) { if (self.delegate && [self.delegate respondsToSelector:@selector(recommendClickCell:atIndexPath:offset:)]) { [self.delegate recommendClickCell:self atIndexPath:self.indexPath offset:indexPath.row]; } } } #pragma mark - Method + (CGFloat)height { return ([FKRecoUserRecoProductCell cellHeight:UISCREENWIDTH]); } + (int)imgLength: (CGFloat)screenWidth { if (screenWidth <= 320) { return 100; } return 130; } + (int)cellHeight:(CGFloat)screenWidth { return (10 + [FKRecoUserRecoProductCell imgLength:screenWidth] + 57); } - (FKHotProductItem *)itemAtIndex:(NSInteger)index { if (index >=0 && index < self.dataSource.count) { return self.dataSource[index]; } return nil; } #pragma mark - Layout - (void)addAllSubviews { [self.contentView addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.contentView); }]; } #pragma mark - Property - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.headerReferenceSize = CGSizeMake(15, 40); layout.footerReferenceSize = CGSizeMake(10, 40); layout.minimumInteritemSpacing = 0; layout.minimumLineSpacing = 10; layout.itemSize = CGSizeMake([FKRecoUserRecoProductCell imgLength:UISCREENWIDTH], [FKRecoUserRecoProductCell cellHeight:UISCREENWIDTH]); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = [UIColor whiteColor]; _collectionView.alwaysBounceVertical = NO; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.scrollsToTop = NO; [_collectionView registerClass:[FKRecoUserCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([FKRecoUserCollectionViewCell class])]; } return _collectionView; } @end