NYQuestionContentViewModel.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // NYQuestionContentViewModel.m
  3. // jiaPei
  4. //
  5. // Created by Ning.ge on 2024/10/31.
  6. // Copyright © 2024 JCZ. All rights reserved.
  7. //
  8. #import "NYQuestionContentViewModel.h"
  9. @interface NYQuestionContentViewModel ()
  10. @end
  11. @implementation NYQuestionContentViewModel
  12. - (void)loadQuestionContentView:(RQExerciseModel *)exerciseModel tableView:(UITableView *)tableView {
  13. [self.dataSource removeAllObjects];
  14. RQCommonGroupViewModel *group0 = [RQCommonGroupViewModel groupViewModel];
  15. NYExerciseQuestionItemViewModel *exerciseQuestionItemViewModel = [[NYExerciseQuestionItemViewModel alloc] initWithRQExerciseModel:exerciseModel];
  16. group0.itemViewModels = @[exerciseQuestionItemViewModel];
  17. [self.dataSource addObject:group0];
  18. @weakify(self)
  19. RQCommonGroupViewModel *group1 = [RQCommonGroupViewModel groupViewModel];
  20. NSArray *allOptionsArr = [@[exerciseModel.ydtQuestionModel.An1,
  21. exerciseModel.ydtQuestionModel.An2,
  22. exerciseModel.ydtQuestionModel.An3,
  23. exerciseModel.ydtQuestionModel.An4,
  24. ].rac_sequence.signal filter:^BOOL(NSString *optionString) {
  25. return RQStringIsNotEmpty(optionString);
  26. }].toArray;
  27. group1.itemViewModels = [allOptionsArr.rac_sequence.signal map:^id _Nullable(NSString *optionString) {
  28. @strongify(self)
  29. return [[NYExerciseOptionItemViewModel alloc] initWithRQExerciseModel:exerciseModel allOptionsArr:allOptionsArr optString:optionString];
  30. }].toArray;
  31. [self.dataSource addObject:group1];
  32. [tableView reloadData];
  33. }
  34. #pragma mark - LazyLoad
  35. - (NSMutableArray *)dataSource {
  36. if(!_dataSource){
  37. _dataSource = [NSMutableArray new];
  38. }
  39. return _dataSource;
  40. }
  41. - (void)configureCell:(RQCommonCollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
  42. [cell bindViewModel:object];
  43. }
  44. #pragma mark - UITableViewDataSource
  45. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  46. return self.dataSource ? self.dataSource.count : 0;
  47. }
  48. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  49. return [((RQCommonGroupViewModel *)self.dataSource[section]).itemViewModels count];
  50. }
  51. - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
  52. RQCommonGroupViewModel *groupViewModel = self.dataSource[indexPath.section];
  53. RQCommonCollectionItemViewModel *itemViewModel = groupViewModel.itemViewModels[indexPath.row];
  54. return [NSClassFromString(itemViewModel.itemClassName) cellWithTableView:tableView];
  55. }
  56. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  57. /// fetch cell
  58. RQCommonCell *cell = (RQCommonCell *)[self tableView:tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
  59. RQCommonGroupViewModel *groupViewModel = self.dataSource[indexPath.section];
  60. id object = groupViewModel.itemViewModels[indexPath.row];
  61. /// bind model
  62. [self configureCell:cell atIndexPath:indexPath withObject:(id)object];
  63. [cell setIndexPath:indexPath rowsInSection:groupViewModel.itemViewModels.count];
  64. return cell;
  65. }
  66. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  67. RQCommonGroupViewModel *groupViewModel = self.dataSource[indexPath.section];
  68. RQCommonItemViewModel *itemViewModel = groupViewModel.itemViewModels[indexPath.row];
  69. CGFloat rowH = itemViewModel.rowHeight;
  70. NSLog(@"%@, rowH= %f",indexPath,rowH);
  71. return rowH; //itemViewModel.rowHeight;
  72. }
  73. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  74. RQCommonGroupViewModel *groupViewModel = self.dataSource[section];
  75. if (groupViewModel.groupModel) {
  76. return groupViewModel.groupModel.headerHeight;
  77. } else {
  78. return groupViewModel.headerHeight;
  79. }
  80. }
  81. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  82. RQCommonGroupViewModel *groupViewModel = self.dataSource[section];
  83. if (groupViewModel.groupModel) {
  84. return groupViewModel.groupModel.footerHeight;
  85. } else {
  86. return groupViewModel.footerHeight;
  87. }
  88. }
  89. #pragma mark UITableViewDelegate
  90. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  91. {
  92. NSLog(@"indexPath==%zd",indexPath.row);
  93. }
  94. @end