RQCommonCollectionViewController.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // RQCommonCollectionViewController.m
  3. // YueXueChe
  4. //
  5. // Created by 张嵘 on 2018/12/19.
  6. // Copyright © 2018 lee. All rights reserved.
  7. //
  8. #import "RQCommonCollectionViewController.h"
  9. #import "RQCommonCollectionViewCell.h"
  10. #import "RQCommonReusableView.h"
  11. @interface RQCommonCollectionViewController ()
  12. /// viewModel
  13. @property (nonatomic, readwrite, strong) RQCommonCollectionViewModel *viewModel;
  14. @end
  15. @implementation RQCommonCollectionViewController
  16. @dynamic viewModel;
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. }
  20. #pragma mark - Override
  21. - (void)bindViewModel {
  22. [super bindViewModel];
  23. }
  24. - (UIEdgeInsets)contentInset {
  25. return UIEdgeInsetsMake((RQ_APPLICATION_NAV_BAR_HEIGHT + RQ_APPLICATION_STATUS_BAR_HEIGHT), 0, 0, 0);
  26. }
  27. - (void)configureCell:(RQCommonCollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
  28. [cell bindViewModel:object];
  29. }
  30. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
  31. return [RQCommonCollectionViewCell cellWithCollectionView:collectionView forIndexPath:indexPath];
  32. }
  33. #pragma mark - UICollectionViewDelegate & UICollectionViewDataSource
  34. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  35. return self.viewModel.dataSource.count;
  36. }
  37. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  38. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section];
  39. return groupViewModel.itemViewModels.count;
  40. }
  41. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  42. /// fetch cell
  43. RQCommonCollectionViewCell *cell = (RQCommonCollectionViewCell *)[self collectionView:collectionView dequeueReusableCellWithIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
  44. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section];
  45. id object = groupViewModel.itemViewModels[indexPath.row];
  46. /// bind model
  47. [self configureCell:cell atIndexPath:indexPath withObject:(id)object];
  48. [cell setIndexPath:indexPath rowsInSection:groupViewModel.itemViewModels.count];
  49. return cell;
  50. }
  51. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  52. // 如果是头视图
  53. if (kind == UICollectionElementKindSectionHeader) {
  54. RQCommonReusableView *headerView = [RQCommonReusableView reusableViewWithCollectionView:collectionView OfKind:kind forIndexPath:indexPath];
  55. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section];
  56. [headerView bindViewModel:groupViewModel];
  57. headerView.headerContentLabel.hidden = NO;
  58. headerView.footerContentLabel.hidden = YES;
  59. return headerView;
  60. }else {
  61. RQCommonReusableView *footerView = [RQCommonReusableView reusableViewWithCollectionView:collectionView OfKind:kind forIndexPath:indexPath];
  62. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section];
  63. [footerView bindViewModel:groupViewModel];
  64. footerView.headerContentLabel.hidden = YES;
  65. footerView.footerContentLabel.hidden = NO;
  66. return footerView;
  67. }
  68. }
  69. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  70. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section];
  71. RQCommonCollectionItemViewModel *itemViewModel = groupViewModel.itemViewModels[indexPath.row];
  72. return itemViewModel.itemSize;
  73. }
  74. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  75. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section];
  76. if (groupViewModel.groupModel) {
  77. return CGSizeMake(RQ_SCREEN_WIDTH, groupViewModel.groupModel.headerHeight);
  78. } else {
  79. return CGSizeMake(RQ_SCREEN_WIDTH, groupViewModel.headerHeight);
  80. }
  81. }
  82. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  83. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section];
  84. if (groupViewModel.groupModel) {
  85. return CGSizeMake(RQ_SCREEN_WIDTH, groupViewModel.groupModel.footerHeight);
  86. } else {
  87. return CGSizeMake(RQ_SCREEN_WIDTH, groupViewModel.footerHeight);
  88. }
  89. }
  90. @end