// // NYQuestionContentView.m // jiaPei // // Created by Ning.ge on 2024/10/30. // Copyright © 2024 JCZ. All rights reserved. // #import "NYQuestionContentView.h" @interface NYQuestionContentView () @property (nonatomic,weak) UIView *bottom_tools; @property (weak, nonatomic) UILabel *titleLabel; @property (weak, nonatomic) UILabel *selAnswerlabel; @property (nonatomic,weak) UIView *answer_view; @property (nonatomic,weak) RQExerciseModel *exerciseModel; @property (nonatomic,strong) UIButton *current_btn; @end @implementation NYQuestionContentView #pragma mark --UI - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setupUI]; } return self; } - (instancetype)initWithCoder:(NSCoder *)decoder { self = [super initWithCoder:decoder]; if (self) { [self setupUI]; } return self; } - (void)setupUI { [self bottomUI]; _questionContentViewModel = [[NYQuestionContentViewModel alloc] init]; RQTableView *tableView = [[RQTableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain]; tableView.backgroundColor = [UIColor qmui_colorWithHexString:@"#F5F6F8"]; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // set delegate and dataSource tableView.delegate = _questionContentViewModel; tableView.dataSource = _questionContentViewModel; [self addSubview:tableView]; self.me_tableView = tableView; // 注册cell [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"]; [tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self); make.bottom.mas_equalTo(self.bottom_tools.mas_top); make.left.mas_equalTo(self).offset(0); make.right.mas_equalTo(self).offset(0); }]; } - (void)bottomUI{ UIView *bottom_tools = [[UIView alloc] init]; bottom_tools.backgroundColor = [UIColor qmui_colorWithHexString:@"#F5F6F8"]; [self addSubview:bottom_tools]; self.bottom_tools = bottom_tools; [bottom_tools mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(self); make.left.mas_equalTo(self).offset(0); make.right.mas_equalTo(self).offset(0); make.height.mas_equalTo(30); }]; UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.backgroundColor = UIColor.clearColor; titleLabel.text = @"您选择的答案:"; titleLabel.font = [UIFont boldSystemFontOfSize:14.f]; titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#2B6BCC"]; [bottom_tools addSubview:titleLabel]; self.titleLabel = titleLabel; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(bottom_tools); make.left.mas_equalTo(bottom_tools).offset(9); make.height.mas_equalTo(30); make.width.mas_equalTo(98); }]; UILabel *selAnswerlabel = [[UILabel alloc] init]; selAnswerlabel.backgroundColor = UIColor.clearColor; selAnswerlabel.text = @""; selAnswerlabel.font = [UIFont systemFontOfSize:14]; selAnswerlabel.textColor = [UIColor qmui_colorWithHexString:@"#BC322D"]; [bottom_tools addSubview:selAnswerlabel]; self.selAnswerlabel = selAnswerlabel; [selAnswerlabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(bottom_tools); make.left.mas_equalTo(titleLabel.mas_right).offset(9); make.height.mas_equalTo(30); make.width.mas_equalTo(80); }]; UIView *answer_view = [[UIView alloc] init]; answer_view.backgroundColor = UIColor.clearColor; [bottom_tools addSubview:answer_view]; self.answer_view = answer_view; [answer_view mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.top.mas_equalTo(bottom_tools); make.right.mas_equalTo(bottom_tools).offset(0); if(iPhone6){ make.width.mas_equalTo(130); }else{ make.width.mas_equalTo(180); } }]; } - (void)readLodData:(RQExerciseModel *)exerciseModel{ self.exerciseModel = exerciseModel; [_questionContentViewModel loadQuestionContentView:exerciseModel tableView:self.me_tableView ]; //选项 if(_questionContentViewModel.dataSource.count>1){ [self.answer_view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; NSArray *allOptionsArr = ((RQCommonGroupViewModel*)_questionContentViewModel.dataSource[1]).itemViewModels; [self showselAnswerlabel]; NSMutableArray *btn_list = [NSMutableArray array]; if(allOptionsArr.count>0){ CGFloat buttonWidth = 25; CGFloat spacing = 10; for (int i = 0; i < allOptionsArr.count; i++) { NYExerciseOptionItemViewModel *item = allOptionsArr[i]; NSString *title = item.title; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.layer.borderWidth = 1.f; button.layer.borderColor = [UIColor qmui_colorWithHexString:@"#3D3C38"].CGColor; button.frame = CGRectMake(i * (buttonWidth + spacing), 2.5, buttonWidth, buttonWidth); button.titleLabel.font = [UIFont systemFontOfSize:14]; [button setTitle:title forState:UIControlStateNormal]; [button setTitleColor:UIColor.blackColor forState:UIControlStateNormal]; [button setTitleColor:UIColor.whiteColor forState:UIControlStateSelected]; [button setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor size:CGSizeMake(100, 100)] forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage imageWithColor:UIColorHex(0x3169CE) size:CGSizeMake(100, 100)] forState:UIControlStateSelected]; [button addTarget:self action:@selector(optionsBtnClickdo:) forControlEvents:UIControlEventTouchUpInside]; [self.answer_view addSubview:button]; [btn_list addObject:button]; } if(self.exerciseModel.userAnswer.count>0){//已答题-选中 if(![self.exerciseModel.userAnswer containsObject:@"预选Option"]){ for (NSString *optString in self.exerciseModel.userAnswer) { NSString *userTitle = @""; if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) { userTitle = @"A"; } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) { userTitle = @"B"; } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) { userTitle = @"C"; } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) { userTitle = @"D"; } for (UIButton *btn in btn_list) { NSLog(@"btnT =%@ uT= %@,",btn.currentTitle,userTitle); if([userTitle isEqualToString:btn.currentTitle]){ btn.selected = YES; } } } } } if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题 //添加一个确认按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.tag = 99; button.layer.borderWidth = 1.f; button.layer.borderColor = [UIColor qmui_colorWithHexString:@"#3D3C38"].CGColor; button.frame = CGRectMake(4 * (buttonWidth + spacing), 2.5, buttonWidth+10, buttonWidth); button.titleLabel.font = [UIFont systemFontOfSize:12]; [button setTitle:@"确认" forState:UIControlStateNormal]; [button setTitleColor:UIColor.blackColor forState:UIControlStateNormal]; [button setTitleColor:UIColor.whiteColor forState:UIControlStateSelected]; [button setBackgroundImage:[UIImage imageWithColor:UIColor.whiteColor size:CGSizeMake(100, 100)] forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage imageWithColor:UIColorHex(0x3169CE) size:CGSizeMake(100, 100)] forState:UIControlStateSelected]; [button addTarget:self action:@selector(affirmBtnClickdo:) forControlEvents:UIControlEventTouchUpInside]; [self.answer_view addSubview:button]; } } } } //确认 - (void)affirmBtnClickdo:(UIButton*)btn{ if(self.exerciseModel.answerResultsType != RQAnswerResultsType_Unanswered)return;//已答题 不可用 NSArray *allOptionsArr = [@[self.exerciseModel.ydtQuestionModel.An1,self.exerciseModel.ydtQuestionModel.An2,self.exerciseModel.ydtQuestionModel.An3,self.exerciseModel.ydtQuestionModel.An4].rac_sequence.signal filter:^BOOL(NSString *optStr) { return RQStringIsNotEmpty(optStr); }].toArray; NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy; if ([self.exerciseModel.userAnswer containsObject:@"预选Option"]) { [userAnswerArr removeObject:@"预选Option"]; } if(userAnswerArr.count==0){ ShowMsg(@"请选择您的答案"); return; } self.exerciseModel.userAnswer = userAnswerArr.copy; NSString *userAnswerIndexStr = [[[userAnswerArr.rac_sequence.signal map:^id _Nullable(NSString *optStr) { return @([allOptionsArr indexOfObject:optStr] + 1); }].toArray arraySortASC] componentsJoinedByString:@""]; [NYTools saveExerciseModel:self.exerciseModel userKey:RQ_USER_MANAGER.currentUser.outId carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQ_Exercise_Module.currentExerciseType];//记录用户做的题目 if (userAnswerIndexStr.integerValue == self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue) { self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct; //做对自动下一题 [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"0"]; if (RQ_Exercise_Module.isRightAutoRemoveError) { [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) { [RQ_YDT_USER_Question_Module doQuestionWithID:self.exerciseModel.ydtQuestionModel.ID myAnswer:userAnswerIndexStr.integerValue trueAnswer:self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQExerciseType_Sequential]; } error:^(NSError * _Nullable error) { }]; } } else {//做错提示 self.exerciseModel.answerResultsType = RQAnswerResultsType_Error; [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"1"]; [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) { [RQ_YDT_USER_Question_Module doQuestionWithID:self.exerciseModel.ydtQuestionModel.ID myAnswer:userAnswerIndexStr.integerValue trueAnswer:self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQExerciseType_Sequential]; } error:^(NSError * _Nullable error) { }]; } } //选项 - (void)optionsBtnClickdo:(UIButton*)btn{ if(self.exerciseModel.answerResultsType != RQAnswerResultsType_Unanswered)return;//已答题 不可用 if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题 btn.selected = !btn.selected; }else{ if(self.current_btn){ self.current_btn.selected = NO; } btn.selected = !btn.selected; } self.current_btn = btn; NSString *optString = @""; NSArray *allOptionsArr = [@[self.exerciseModel.ydtQuestionModel.An1,self.exerciseModel.ydtQuestionModel.An2,self.exerciseModel.ydtQuestionModel.An3,self.exerciseModel.ydtQuestionModel.An4].rac_sequence.signal filter:^BOOL(NSString *optStr) { return RQStringIsNotEmpty(optStr); }].toArray; if([btn.currentTitle isEqualToString:@"A"]){ optString = allOptionsArr[0]; }else if([btn.currentTitle isEqualToString:@"B"]){ optString = allOptionsArr[1]; }else if([btn.currentTitle isEqualToString:@"C"]){ optString = allOptionsArr[2]; }else if([btn.currentTitle isEqualToString:@"D"]){ optString = allOptionsArr[3]; } if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题 if (![self.exerciseModel.userAnswer containsObject:optString]) { NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy; [userAnswerArr addObject:optString]; self.exerciseModel.userAnswer = userAnswerArr.copy; } else { NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy; [userAnswerArr removeObject:optString]; self.exerciseModel.userAnswer = userAnswerArr.copy; } [self showselAnswerlabel]; } else { self.exerciseModel.userAnswer = @[optString].mutableCopy; [self showselAnswerlabel]; [NYTools saveExerciseModel:self.exerciseModel userKey:RQ_USER_MANAGER.currentUser.outId carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQ_Exercise_Module.currentExerciseType];//记录用户做的题目 if (self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue == [allOptionsArr indexOfObject:optString] + 1) {//做对自动下一题 self.exerciseModel.answerResultsType = RQAnswerResultsType_Correct; [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"0"]; if (RQ_Exercise_Module.isRightAutoRemoveError) { [[[RQ_HTTP_Service deleteFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) { [RQ_YDT_USER_Question_Module doQuestionWithID:self.exerciseModel.ydtQuestionModel.ID myAnswer:[allOptionsArr indexOfObject:optString] + 1 trueAnswer:self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQExerciseType_Sequential]; } error:^(NSError * _Nullable error) { }]; } } else {//做错提示 self.exerciseModel.answerResultsType = RQAnswerResultsType_Error; [RQNotificationCenter postNotificationName:NYExerciseOptionDTNotification object:@"1"]; [[[RQ_HTTP_Service saveFavQuestionWithQuestionId:self.exerciseModel.ydtQuestionModel.ID type:RQSaveFavQuestionType_Wrong] deliverOnMainThread] subscribeNext:^(id _Nullable x) { [RQ_YDT_USER_Question_Module doQuestionWithID:self.exerciseModel.ydtQuestionModel.ID myAnswer:[allOptionsArr indexOfObject:optString] + 1 trueAnswer:self.exerciseModel.ydtQuestionModel.AnswerTrue.integerValue carType:RQ_YDTQuestion_Module.carType subject:RQ_YDTQuestion_Module.subject exerciseType:RQExerciseType_Sequential]; } error:^(NSError * _Nullable error) { }]; } } } //显示用户选择答案 - (void)showselAnswerlabel{ NSMutableString *answer_str = [NSMutableString string]; if (self.exerciseModel.ydtQuestionModel.Type == 3) {//多选题 NSMutableArray *userAnswerArr = self.exerciseModel.userAnswer.mutableCopy; [userAnswerArr removeObject:@"预选Option"]; self.exerciseModel.userAnswer = userAnswerArr.copy; } if(self.exerciseModel.userAnswer.count>0&&![self.exerciseModel.userAnswer containsObject:@"预选Option"]){ for (NSString *optString in self.exerciseModel.userAnswer) { if ([self.exerciseModel.ydtQuestionModel.An1 isEqualToString:optString]) { [answer_str appendString:@"A,"]; } else if ([self.exerciseModel.ydtQuestionModel.An2 isEqualToString:optString]) { [answer_str appendString:@"B,"]; } else if ([self.exerciseModel.ydtQuestionModel.An3 isEqualToString:optString]) { [answer_str appendString:@"C,"]; } else if ([self.exerciseModel.ydtQuestionModel.An4 isEqualToString:optString]) { [answer_str appendString:@"D,"]; } } [answer_str deleteCharactersInRange:NSMakeRange(answer_str.length - 1, 1)]; } self.selAnswerlabel.text = answer_str; self.exerciseModel.userAnswerStr = answer_str; } @end