// // RQCommonViewController.m // RQCommon // // Created by 张嵘 on 2018/11/27. // Copyright © 2018 张嵘. All rights reserved. // #import "RQCommonViewController.h" #import "RQCommonHeaderView.h" #import "RQCommonFooterView.h" #import "RQCommonCell.h" @interface RQCommonViewController () /// viewModel @property (nonatomic, readwrite, strong) RQCommonViewModel *viewModel; @end @implementation RQCommonViewController @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:(RQCommonCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object{ [cell bindViewModel:object]; } - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{ return [RQCommonCell cellWithTableView:tableView]; } #pragma mark - UITableViewDelegate & UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.viewModel.dataSource.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section]; return groupViewModel.itemViewModels.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ /// fetch cell RQCommonCell *cell = (RQCommonCell *)[self tableView:tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" 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; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ RQCommonFooterView *footerView = [RQCommonFooterView footerViewWithTableView:tableView]; RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section]; [footerView bindViewModel:groupViewModel]; return footerView; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ RQCommonHeaderView *headerView = [RQCommonHeaderView headerViewWithTableView:tableView]; RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section]; [headerView bindViewModel:groupViewModel]; return headerView; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section]; RQCommonItemViewModel *itemViewModel = groupViewModel.itemViewModels[indexPath.row]; return itemViewModel.rowHeight; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section]; if (groupViewModel.groupModel) { return groupViewModel.groupModel.headerHeight; } else { return groupViewModel.headerHeight; } } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[section]; if (groupViewModel.groupModel) { return groupViewModel.groupModel.footerHeight; } else { return groupViewModel.footerHeight; } } @end