// // NYQuestionContentViewModel.m // jiaPei // // Created by Ning.ge on 2024/10/31. // Copyright © 2024 JCZ. All rights reserved. // #import "NYQuestionContentViewModel.h" @interface NYQuestionContentViewModel () @end @implementation NYQuestionContentViewModel - (void)loadQuestionContentView:(RQExerciseModel *)exerciseModel tableView:(UITableView *)tableView { [self.dataSource removeAllObjects]; RQCommonGroupViewModel *group0 = [RQCommonGroupViewModel groupViewModel]; NYExerciseQuestionItemViewModel *exerciseQuestionItemViewModel = [[NYExerciseQuestionItemViewModel alloc] initWithRQExerciseModel:exerciseModel]; group0.itemViewModels = @[exerciseQuestionItemViewModel]; [self.dataSource addObject:group0]; @weakify(self) RQCommonGroupViewModel *group1 = [RQCommonGroupViewModel groupViewModel]; NSArray *allOptionsArr = [@[exerciseModel.ydtQuestionModel.An1, exerciseModel.ydtQuestionModel.An2, exerciseModel.ydtQuestionModel.An3, exerciseModel.ydtQuestionModel.An4, ].rac_sequence.signal filter:^BOOL(NSString *optionString) { return RQStringIsNotEmpty(optionString); }].toArray; group1.itemViewModels = [allOptionsArr.rac_sequence.signal map:^id _Nullable(NSString *optionString) { @strongify(self) return [[NYExerciseOptionItemViewModel alloc] initWithRQExerciseModel:exerciseModel allOptionsArr:allOptionsArr optString:optionString]; }].toArray; [self.dataSource addObject:group1]; [tableView reloadData]; } #pragma mark - LazyLoad - (NSMutableArray *)dataSource { if(!_dataSource){ _dataSource = [NSMutableArray new]; } return _dataSource; } - (void)configureCell:(RQCommonCollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object { [cell bindViewModel:object]; } #pragma mark - UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.dataSource ? self.dataSource.count : 0; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [((RQCommonGroupViewModel *)self.dataSource[section]).itemViewModels count]; } - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath { RQCommonGroupViewModel *groupViewModel = self.dataSource[indexPath.section]; RQCommonCollectionItemViewModel *itemViewModel = groupViewModel.itemViewModels[indexPath.row]; return [NSClassFromString(itemViewModel.itemClassName) cellWithTableView:tableView]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ /// fetch cell RQCommonCell *cell = (RQCommonCell *)[self tableView:tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath]; RQCommonGroupViewModel *groupViewModel = self.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; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ RQCommonGroupViewModel *groupViewModel = self.dataSource[indexPath.section]; RQCommonItemViewModel *itemViewModel = groupViewModel.itemViewModels[indexPath.row]; CGFloat rowH = itemViewModel.rowHeight; NSLog(@"%@, rowH= %f",indexPath,rowH); return rowH; //itemViewModel.rowHeight; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ RQCommonGroupViewModel *groupViewModel = self.dataSource[section]; if (groupViewModel.groupModel) { return groupViewModel.groupModel.headerHeight; } else { return groupViewModel.headerHeight; } } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ RQCommonGroupViewModel *groupViewModel = self.dataSource[section]; if (groupViewModel.groupModel) { return groupViewModel.groupModel.footerHeight; } else { return groupViewModel.footerHeight; } } #pragma mark UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"indexPath==%zd",indexPath.row); } @end