// // RQCommonCollectionViewController.m // YueXueChe // // Created by 张嵘 on 2018/12/19. // Copyright © 2018 lee. All rights reserved. // #import "RQCommonCollectionViewController.h" #import "RQCommonCollectionViewCell.h" #import "RQCommonReusableView.h" @interface RQCommonCollectionViewController () /// viewModel @property (nonatomic, readwrite, strong) RQCommonCollectionViewModel *viewModel; @end @implementation RQCommonCollectionViewController @dynamic viewModel; - (void)viewDidLoad { [super viewDidLoad]; } #pragma mark - Override - (void)bindViewModel { [super bindViewModel]; } - (UIEdgeInsets)contentInset { return UIEdgeInsetsMake((RQ_APPLICATION_NAV_BAR_HEIGHT + RQ_APPLICATION_STATUS_BAR_HEIGHT), 0, 0, 0); } - (void)configureCell:(RQCommonCollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object { [cell bindViewModel:object]; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath { return [RQCommonCollectionViewCell cellWithCollectionView:collectionView forIndexPath:indexPath]; } #pragma mark - UICollectionViewDelegate & UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return self.viewModel.dataSource.count; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section]; return groupViewModel.itemViewModels.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { /// fetch cell RQCommonCollectionViewCell *cell = (RQCommonCollectionViewCell *)[self collectionView:collectionView dequeueReusableCellWithIdentifier:@"UICollectionViewCell" forIndexPath:indexPath]; RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section]; id object = groupViewModel.itemViewModels[indexPath.row]; /// bind model [self configureCell:cell atIndexPath:indexPath withObject:(id)object]; [cell setIndexPath:indexPath rowsInSection:groupViewModel.itemViewModels.count]; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { // 如果是头视图 if (kind == UICollectionElementKindSectionHeader) { RQCommonReusableView *headerView = [RQCommonReusableView reusableViewWithCollectionView:collectionView OfKind:kind forIndexPath:indexPath]; RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section]; [headerView bindViewModel:groupViewModel]; headerView.headerContentLabel.hidden = NO; headerView.footerContentLabel.hidden = YES; return headerView; }else { RQCommonReusableView *footerView = [RQCommonReusableView reusableViewWithCollectionView:collectionView OfKind:kind forIndexPath:indexPath]; RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section]; [footerView bindViewModel:groupViewModel]; footerView.headerContentLabel.hidden = YES; footerView.footerContentLabel.hidden = NO; return footerView; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section]; RQCommonCollectionItemViewModel *itemViewModel = groupViewModel.itemViewModels[indexPath.row]; return itemViewModel.itemSize; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section]; if (groupViewModel.groupModel) { return CGSizeMake(RQ_SCREEN_WIDTH, groupViewModel.groupModel.headerHeight); } else { return CGSizeMake(RQ_SCREEN_WIDTH, groupViewModel.headerHeight); } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section]; if (groupViewModel.groupModel) { return CGSizeMake(RQ_SCREEN_WIDTH, groupViewModel.groupModel.footerHeight); } else { return CGSizeMake(RQ_SCREEN_WIDTH, groupViewModel.footerHeight); } } @end