// // FKExploreTableCell.m // FirstLink // // Created by ascii on 16/2/16. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKExploreTableViewCell.h" #import "FKExploreTopicListItem.h" #import "FKExploreNormalCell.h" #import "FKExploreMoreCell.h" #import "FKRecommendViewModel.h" @interface FKExploreTableViewCell () @property (nonatomic, strong) UIImageView *largeImageView; @property (nonatomic, strong) UIImageView *categoryImgIcon; @property (nonatomic, strong) UIImageView *triangleImgIcon; @property (nonatomic, strong) FKExploreTopicListItem *topicListItem; @end @implementation FKExploreTableViewCell - (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)addAllSubviews { [self.contentView addSubview:self.largeImageView]; [self.largeImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.equalTo(self.contentView); make.height.mas_equalTo([FKExploreTableViewCell largeImageHeight:UISCREENWIDTH]); }]; [self.largeImageView addSubview:self.categoryImgIcon]; [self.categoryImgIcon mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.largeImageView).offset(10); make.left.equalTo(self.largeImageView); make.size.mas_equalTo(CGSizeMake(59, 16)); }]; [self.largeImageView addSubview:self.triangleImgIcon]; [self.triangleImgIcon mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.largeImageView); make.left.equalTo(self.largeImageView).offset([FKExploreTableViewCell thumbImageBoardLength:UISCREENWIDTH]/2 + 15 - 10); }]; [self.contentView addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.right.equalTo(self.contentView); make.top.equalTo(self.largeImageView.mas_bottom); }]; } - (void)configCell:(FKExploreTopicListItem *)item { [self.largeImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", item.photoURL, [FLStringHelper cdnParamaterString:UISCREENWIDTH height:[FKExploreTableViewCell largeImageHeight:UISCREENWIDTH]]]]]; self.topicListItem = item; [self.collectionView reloadData]; } - (void)fk_configWithViewModel:(FKRecommendViewModel *)viewModel indexPath:(NSIndexPath *)indexPath { if ([viewModel isKindOfClass:[FKRecommendViewModel class]]) { if ((indexPath.row - 1) < viewModel.operationActivityArea.dataArray.count) { FKActivityItem *activityItem = viewModel.operationActivityArea.dataArray[indexPath.row - 1]; FKExploreTopicListItem *topicListItem = [FKExploreTopicListItem new]; topicListItem.topicID = activityItem.itemID; topicListItem.photoURL = activityItem.picUrl; topicListItem.objectType = @"10"; topicListItem.categoryURL = activityItem.categoryURL; NSMutableArray *array = [NSMutableArray array]; FKExploreTopicListSubItem *subItem; for (FKActivityProductItem *productItem in activityItem.productArray) { subItem = [FKExploreTopicListSubItem new]; subItem.itemID = productItem.itemID; subItem.name = productItem.title; subItem.indexPic = productItem.picURL; subItem.price = productItem.price; subItem.referPrice = productItem.originalPrice; [array addObject:subItem]; } topicListItem.subItemArray = array; self.topicListItem = topicListItem; if (topicListItem.categoryURL.length > 0) { self.categoryImgIcon.hidden = NO; [self.categoryImgIcon sd_setImageWithURL:[NSURL URLWithString:topicListItem.categoryURL]]; } else { self.categoryImgIcon.hidden = YES; } [self.largeImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", topicListItem.photoURL, [FLStringHelper cdnParamaterString:UISCREENWIDTH height:[FKExploreTableViewCell largeImageHeight:UISCREENWIDTH]]]]]; [self.collectionView reloadData]; self.collectionView.delegate = self; } } } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return (self.topicListItem.subItemArray.count + 1); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell; if (indexPath.row == self.topicListItem.subItemArray.count) { cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FKExploreMoreCell class]) forIndexPath:indexPath]; } else { FKExploreNormalCell *normalCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([FKExploreNormalCell class]) forIndexPath:indexPath]; [normalCell.imageView setImageWithURL:[self.topicListItem subItemImageURLStringAtIndex:indexPath.row] cdnWidth:[FKExploreTableViewCell thumbImageBoardLength:UISCREENWIDTH]]; // normalCell.titleLabel.text = [self.topicListItem subItemTitleAtIndex:indexPath.row]; normalCell.priceLabel.attributedText = [self.topicListItem priceAttributedStringAtIndex:indexPath.row]; cell = normalCell; } return cell; } - (IBAction)clickLargeImageViewGesture:(UIGestureRecognizer *)gesture { if (self.delegate && [self.delegate respondsToSelector:@selector(recommendClickCell:atIndexPath:offset:)]) { [self.delegate recommendClickCell:self atIndexPath:self.indexPath offset:gesture.view.tag]; } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { // 更多Cell使用-1作为index NSInteger index = (indexPath.row == self.topicListItem.subItemArray.count ? -1 : indexPath.row); if (self.delegate && [self.delegate respondsToSelector:@selector(recommendClickCell:atIndexPath:offset:)]) { [self.delegate recommendClickCell:self atIndexPath:self.indexPath offset:index]; } } #pragma mark - Height + (CGFloat)height { return ([FKExploreTableViewCell largeImageHeight:UISCREENWIDTH] + [FKExploreTableViewCell scrollViewHeight:UISCREENWIDTH]); } + (int)largeImageHeight: (CGFloat)screenWidth { return (int)(screenWidth * 185.0 / 375.0); } + (int)thumbImageBoardLength: (CGFloat)screenWidth { if (screenWidth <= 321) { return 100; } return 100; } + (int)scrollViewHeight:(CGFloat)screenWidth { return (10 + [FKExploreTableViewCell thumbImageBoardLength:screenWidth] + 36); } #pragma mark - Property - (UIImageView *)largeImageView { if (!_largeImageView) { _largeImageView = [UIImageView new]; _largeImageView.tag = -1; _largeImageView.userInteractionEnabled = YES; [_largeImageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLargeImageViewGesture:)]]; } return _largeImageView; } - (UIImageView *)categoryImgIcon { if (!_categoryImgIcon) { _categoryImgIcon = [UIImageView new]; } return _categoryImgIcon; } - (UIImageView *)triangleImgIcon { if (!_triangleImgIcon) { _triangleImgIcon = [UIImageView new]; _triangleImgIcon.image = [UIImage imageNamed:@"WhiteTriangleIcon"]; } return _triangleImgIcon; } - (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([FKExploreTableViewCell thumbImageBoardLength:UISCREENWIDTH], [FKExploreTableViewCell scrollViewHeight: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:[FKExploreMoreCell class] forCellWithReuseIdentifier:NSStringFromClass([FKExploreMoreCell class])]; [_collectionView registerClass:[FKExploreNormalCell class] forCellWithReuseIdentifier:NSStringFromClass([FKExploreNormalCell class])]; } return _collectionView; } @end