// // XPMineHeadItemTableViewCell.m // xplan-ios // // Created by GreenLand on 2022/1/19. // #import "XPMineHeadItemTableViewCell.h" ///Third #import ///Tool #import "ThemeColor.h" #import "XPMacro.h" ///View #import "XPMineHeadItemCollectionViewCell.h" @interface XPMineHeadItemTableViewCell () ///列表 @property (nonatomic,strong) UICollectionView *collectionView; @property (nonatomic, strong) UIView *mainView; @property (nonatomic, strong) UIView *slideBackView; @property (nonatomic, strong) UIView *sliderView; @end @implementation XPMineHeadItemTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { self.backgroundColor = [UIColor clearColor]; [self.contentView addSubview:self.mainView]; [self.mainView addSubview:self.collectionView]; } - (void)initSubViewConstraints { [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.contentView).inset(15); make.top.bottom.mas_equalTo(self.contentView); }]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(20); make.left.right.mas_equalTo(self.mainView); make.height.mas_equalTo(77+32+17); }]; } #pragma mark - UICollectionViewDatasource And UICollectionViewDelegate - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.datasource.count; } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { XPMineHeadItemCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPMineHeadItemCollectionViewCell class]) forIndexPath:indexPath]; XPMineFuntionItemModel * item = [self.datasource objectAtIndex:indexPath.row]; cell.itemModel = item; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPMineHeadItemTableViewCell:didSelectItem:)]) { XPMineFuntionItemModel * model = [self.datasource objectAtIndex:indexPath.row]; [self.delegate xPMineHeadItemTableViewCell:self didSelectItem:model]; } } #pragma mark - UICollectionViewFlowlayout - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 0; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat width = (KScreenWidth - 30) / 4; return CGSizeMake(width, 32+4+17); } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 0; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGPoint offset = scrollView.contentOffset; CGRect frame = self.sliderView.frame; frame.origin.x = offset.x*12/(scrollView.contentSize.width-scrollView.frame.size.width); self.sliderView.frame = frame; } #pragma mark - Getters And Setters - (void)setDatasource:(NSArray *)datasource { _datasource = datasource; if(datasource.count > 4) { [self.mainView addSubview:self.slideBackView]; [self.slideBackView addSubview:self.sliderView]; [self.slideBackView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.mainView); make.size.mas_equalTo(CGSizeMake(24, 4)); make.top.mas_equalTo(self.collectionView.mas_bottom).mas_offset(10); }]; [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(12, 4)); make.top.left.bottom.mas_equalTo(self.slideBackView); }]; } else { [self.sliderView removeFromSuperview]; [self.slideBackView removeFromSuperview]; } [self.collectionView reloadData]; } - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.dataSource = self; _collectionView.delegate = self; _collectionView.backgroundColor = [ThemeColor appCellBackgroundColor]; [_collectionView registerClass:[XPMineHeadItemCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPMineHeadItemCollectionViewCell class])]; _collectionView.showsHorizontalScrollIndicator = NO; } return _collectionView; } - (UIView *)sliderView { if (!_sliderView) { _sliderView = [[UIView alloc] init]; _sliderView.backgroundColor = UIColorFromRGB(0xFFBC51); _sliderView.layer.cornerRadius = 2; _sliderView.layer.masksToBounds = YES; } return _sliderView; } - (UIView *)slideBackView { if (!_slideBackView) { _slideBackView = [[UIView alloc] init]; _slideBackView.backgroundColor = UIColorRGBAlpha(0x000000, 0.1); _slideBackView.layer.cornerRadius = 2; _slideBackView.layer.masksToBounds = YES; _slideBackView.clipsToBounds = YES; } return _slideBackView; } - (UIView *)mainView { if (!_mainView) { _mainView = [[UIView alloc] init]; _mainView.backgroundColor = [UIColor whiteColor]; _mainView.layer.cornerRadius = 8; _mainView.layer.masksToBounds = YES; } return _mainView; } @end