RQCommonViewController.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // RQCommonViewController.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/27.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "RQCommonViewController.h"
  9. #import "RQCommonHeaderView.h"
  10. #import "RQCommonFooterView.h"
  11. #import "RQCommonCell.h"
  12. @interface RQCommonViewController ()
  13. /// viewModel
  14. @property (nonatomic, readwrite, strong) RQCommonViewModel *viewModel;
  15. @end
  16. @implementation RQCommonViewController
  17. @dynamic viewModel;
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. }
  21. #pragma mark - Override
  22. - (void)bindViewModel{
  23. [super bindViewModel];
  24. }
  25. - (UIEdgeInsets)contentInset{
  26. return UIEdgeInsetsMake(RQ_APPLICATION_TOP_BAR_HEIGHT , 0, 0, 0);
  27. }
  28. - (void)configureCell:(RQCommonCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object{
  29. [cell bindViewModel:object];
  30. }
  31. - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
  32. return [RQCommonCell cellWithTableView:tableView];
  33. }
  34. #pragma mark - UITableViewDelegate & UITableViewDataSource
  35. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  36. return self.viewModel.dataSource.count;
  37. }
  38. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  39. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section];
  40. return groupViewModel.itemViewModels.count;
  41. }
  42. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  43. /// fetch cell
  44. RQCommonCell *cell = (RQCommonCell *)[self tableView:tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
  45. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section];
  46. id object = groupViewModel.itemViewModels[indexPath.row];
  47. /// bind model
  48. [self configureCell:cell atIndexPath:indexPath withObject:(id)object];
  49. [cell setIndexPath:indexPath rowsInSection:groupViewModel.itemViewModels.count];
  50. return cell;
  51. }
  52. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  53. RQCommonFooterView *footerView = [RQCommonFooterView footerViewWithTableView:tableView];
  54. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section];
  55. [footerView bindViewModel:groupViewModel];
  56. return footerView;
  57. }
  58. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  59. RQCommonHeaderView *headerView = [RQCommonHeaderView headerViewWithTableView:tableView];
  60. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section];
  61. [headerView bindViewModel:groupViewModel];
  62. return headerView;
  63. }
  64. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  65. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section];
  66. RQCommonItemViewModel *itemViewModel = groupViewModel.itemViewModels[indexPath.row];
  67. return itemViewModel.rowHeight;
  68. }
  69. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  70. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section];
  71. return groupViewModel.headerHeight;
  72. }
  73. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  74. RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section];
  75. return groupViewModel.footerHeight;
  76. }
  77. @end