// // KXRecommendView.m // CAISHEN // // Created by kuxuan on 2017/8/28. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXRecommendView.h" #import "KXRecommendCollectionViewCell.h" #import "KXRecommnedModel.h" @implementation KXRecommendView { UICollectionView *_collectionView; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor whiteColor]; [self setupUI]; } return self; } - (void)setupUI { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; flowLayout.itemSize = CGSizeMake(78*SCREEN_MUTI, 90*SCREEN_MUTI); _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout]; _collectionView.backgroundColor = [UIColor whiteColor]; _collectionView.showsHorizontalScrollIndicator = NO; [_collectionView registerClass:[KXRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; _collectionView.dataSource = self; _collectionView.delegate = self; [self addSubview:_collectionView]; } - (void)setDataSource:(NSArray *)dataSource { _dataSource = dataSource; [_collectionView reloadData]; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataSource.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KXRecommendCollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; KXRecommnedModel *model = self.dataSource[indexPath.row]; [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"placeholder_square"] options:0]; cell.titleLabel.text = model.name; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KXRecommnedModel *model = self.dataSource[indexPath.item]; [_delegate recmmendViewClickWithId:model.Id name:model.name]; } @end