JXExamViewController.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. //
  2. // JXExamViewController.m
  3. // shiku_im
  4. //
  5. // Created by qiudezheng on 2020/4/29.
  6. // Copyright © 2020 Reese. All rights reserved.
  7. //
  8. #import "JXExamViewController.h"
  9. #import "JXQuestionTableViewCell.h"
  10. #import "JXApplyViewController.h"
  11. @interface JXExamViewController ()<UIAlertViewDelegate>
  12. @property(strong,nonatomic)UIView * navigationView;
  13. @property(strong,nonatomic)UIView * alertViews;
  14. @property(strong,nonatomic)UILabel * alertLabel;
  15. @property(strong,nonatomic)UILabel * countLabel;
  16. @property(strong,nonatomic)UIButton * backButton;
  17. @property(strong,nonatomic)UILabel * questionLabel;
  18. @property(strong,nonatomic)UILabel * questionTitle;
  19. @property(strong,nonatomic)UITableView * questionTable;
  20. @property(strong,nonatomic)UIScrollView * scrollView;
  21. @property(strong,nonatomic)NSArray * questionArray;
  22. @property(assign,nonatomic)int currentSort;
  23. @property(strong,nonatomic)NSDictionary * currentQuestion;
  24. @property(strong,nonatomic)NSTimer * timer;
  25. @property(assign,nonatomic)int count;
  26. @property (nonatomic,assign) NSInteger indexRightRow;
  27. @end
  28. @implementation JXExamViewController
  29. -(void)viewWillDisappear:(BOOL)animated
  30. {
  31. [SVProgressHUD dismiss];
  32. }
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. _currentQuestion=[NSDictionary dictionary];
  36. self.view.backgroundColor = [UIColor colorWithRed:126/255.0f green:23/255.0f blue:21/255.0f alpha:1.0f];
  37. self.tableView.alpha = 0.0f;
  38. self.currentSort = 1;
  39. [self initNavigationView];
  40. [self initComponents];
  41. [SVProgressHUD show];
  42. self.count = 5;
  43. long time = (long)[[NSDate date] timeIntervalSince1970];
  44. time = (time *1000 + g_server.timeDifference);
  45. NSString *salt = [NSString stringWithFormat:@"%ld", time];
  46. [g_server getAllQuestions:salt andToView:self];
  47. self.alertViews = [[UIView alloc]initWithFrame:CGRectMake(0,0,JX_SCREEN_WIDTH,JX_SCREEN_HEIGHT)];
  48. self.alertViews.backgroundColor = [UIColor blackColor];
  49. self.alertViews.alpha = 0.0f;
  50. [self.view addSubview:self.alertViews];
  51. self.alertLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, JX_SCREEN_HEIGHT/ 2 - 50, self.alertViews.frame.size.width, 100)];
  52. self.alertLabel.text = @"恭喜你答对了";
  53. self.alertLabel.textColor = [UIColor whiteColor];
  54. self.alertLabel.font = [UIFont systemFontOfSize:16.0f];
  55. self.alertLabel.textAlignment = NSTextAlignmentCenter;
  56. self.alertLabel.numberOfLines = 2;
  57. [self.alertViews addSubview:self.alertLabel];
  58. self.countLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, self.alertLabel.frame.size.height + self.alertLabel.frame.origin.y, self.alertViews.frame.size.width, 20)];
  59. self.countLabel.text = @"5s";
  60. self.countLabel.textColor = [UIColor whiteColor];
  61. self.countLabel.font = [UIFont systemFontOfSize:14.0f];
  62. self.countLabel.textAlignment = NSTextAlignmentCenter;
  63. [self.alertViews addSubview:self.countLabel];
  64. if([self.ifAnswer isEqualToString:@"NO"])
  65. {
  66. UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您已完成今日答题" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
  67. [alert show];
  68. }
  69. }
  70. #pragma mark --- 提交
  71. -(void)postButtonClicked:(UIButton *)sender
  72. {
  73. if([self.ifAnswer isEqualToString:@"NO"])
  74. {
  75. [SVProgressHUD setMaximumDismissTimeInterval:2];
  76. [SVProgressHUD showErrorWithStatus:@"已经完成所有答题"];
  77. }
  78. else
  79. {
  80. NSMutableArray * mutableArray = [[NSMutableArray alloc]initWithCapacity:0];
  81. for(NSDictionary * dict in self.questionArray)
  82. {
  83. NSString * lastAnswer = [NSString stringWithFormat:@"%@",[dict objectForKey:@"answerid"]];
  84. if(lastAnswer != nil && lastAnswer.length != 0 && ![lastAnswer isEqualToString:@"(null)"] && ![lastAnswer isEqualToString:@"<null>"])
  85. {
  86. NSDictionary * dictionary = [[NSDictionary alloc]initWithObjectsAndKeys:[NSString stringWithFormat:@"%@",[dict objectForKey:@"id"]],@"questionId",lastAnswer,@"optionId", nil];
  87. [mutableArray addObject:dictionary];
  88. long time = (long)[[NSDate date] timeIntervalSince1970];
  89. time = (time *1000 + g_server.timeDifference);
  90. NSString *salt = [NSString stringWithFormat:@"%ld", time];
  91. NSArray * postArray = mutableArray.copy;
  92. NSError *parseError = nil;
  93. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postArray options:NSJSONWritingPrettyPrinted error:&parseError];
  94. NSString *jsonstr =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  95. [SVProgressHUD show];
  96. [g_server postQuestionAnswers:salt andOptions:jsonstr andToView:self];
  97. }
  98. else
  99. {
  100. UIAlertView * aler = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"您有题目没有答完,请返回检查"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
  101. [aler show];
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  108. // [_wait hide];
  109. [SVProgressHUD dismiss];
  110. if([aDownload.action isEqualToString:act_GetQuestionAndRules])
  111. {
  112. NSDictionary * dictionary0 = [array1 objectAtIndex:0];
  113. self.questionArray = [dictionary0 objectForKey:@"questions"];
  114. if(self.questionArray.count == 0)
  115. {
  116. self.questionTable.alpha = 0.0f;
  117. }
  118. else
  119. {
  120. for(NSDictionary * dictionary in self.questionArray)
  121. {
  122. NSString * questionSort = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"sort"]];
  123. if(questionSort.intValue == 1)
  124. {
  125. NSString * questionTitle = [NSString stringWithFormat:@"第%@题:%@",questionSort,[dictionary objectForKey:@"title"]];
  126. NSString * questionDesc = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"desc"]];
  127. if(questionTitle != nil && questionTitle.length != 0 && ![questionTitle isEqualToString:@"(null)"] && ![questionTitle isEqualToString:@"<null>"])
  128. {
  129. self.questionTitle.text = questionTitle;
  130. }
  131. if(questionDesc != nil && questionDesc.length != 0 && ![questionDesc isEqualToString:@"(null)"] && ![questionDesc isEqualToString:@"<null>"])
  132. {
  133. self.questionLabel.text = questionDesc;
  134. }
  135. self.currentQuestion = dictionary;
  136. break;
  137. }
  138. }
  139. [self.questionTable reloadData];
  140. }
  141. }
  142. if([aDownload.action isEqualToString:act_PostQuestionAnswers])
  143. {
  144. NSString * rightCount = [NSString stringWithFormat:@"%@",[dict objectForKey:@"rightCount"]];
  145. NSString * coinCount = [NSString stringWithFormat:@"%@",[dict objectForKey:@"coinCount"]];
  146. UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"您一共答对了%@道题,获得金币%@",rightCount,coinCount] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
  147. alertView.tag = 1000;
  148. [alertView show];
  149. [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshes" object:nil];
  150. }
  151. }
  152. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  153. NSString * errorCode = [NSString stringWithFormat:@"%@",[dict objectForKey:@"resultMsg"]];
  154. if([errorCode isEqualToString:@"权限验证失败"])
  155. {
  156. if([aDownload.action isEqualToString:act_GetQuestionAndRules])
  157. {
  158. long time = (long)[[NSDate date] timeIntervalSince1970];
  159. time = (time *1000 + g_server.timeDifference);
  160. NSString *salt = [NSString stringWithFormat:@"%ld", time];
  161. [g_server getAllQuestions:salt andToView:self];
  162. }
  163. if([aDownload.action isEqualToString:act_PostQuestionAnswers])
  164. {
  165. if([self.ifAnswer isEqualToString:@"NO"])
  166. {
  167. [SVProgressHUD setMaximumDismissTimeInterval:2];
  168. [SVProgressHUD showErrorWithStatus:@"已经完成所有答题"];
  169. }
  170. else
  171. {
  172. NSMutableArray * mutableArray = [[NSMutableArray alloc]initWithCapacity:0];
  173. for(NSDictionary * dict in self.questionArray)
  174. {
  175. NSString * lastAnswer = [NSString stringWithFormat:@"%@",[dict objectForKey:@"answerid"]];
  176. if(lastAnswer != nil && lastAnswer.length != 0 && ![lastAnswer isEqualToString:@"(null)"] && ![lastAnswer isEqualToString:@"<null>"])
  177. {
  178. NSDictionary * dictionary = [[NSDictionary alloc]initWithObjectsAndKeys:[NSString stringWithFormat:@"%@",[dict objectForKey:@"id"]],@"questionId",lastAnswer,@"optionId", nil];
  179. [mutableArray addObject:dictionary];
  180. }
  181. else
  182. {
  183. UIAlertView * aler = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"您有题目没有答完,请返回检查"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
  184. [aler show];
  185. break;
  186. }
  187. }
  188. long time = (long)[[NSDate date] timeIntervalSince1970];
  189. time = (time *1000 + g_server.timeDifference);
  190. NSString *salt = [NSString stringWithFormat:@"%ld", time];
  191. NSArray * postArray = mutableArray.copy;
  192. NSError *parseError = nil;
  193. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postArray options:NSJSONWritingPrettyPrinted error:&parseError];
  194. NSString *jsonstr =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  195. [g_server postQuestionAnswers:salt andOptions:jsonstr andToView:self];
  196. }
  197. }
  198. }
  199. else
  200. {
  201. [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",[dict objectForKey:@"resultMsg"]]];
  202. }
  203. if( [aDownload.action isEqualToString:act_UserLogout] ){
  204. [self performSelector:@selector(doSwitch) withObject:nil afterDelay:1];
  205. }
  206. return hide_error;
  207. }
  208. -(void)backButtonClicked:(UIButton *)sender
  209. {
  210. [g_navigation dismissViewController:self animated:YES];
  211. }
  212. -(void)initNavigationView
  213. {
  214. self.navigationView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT>=812?88:64)];
  215. self.navigationView.backgroundColor = [UIColor whiteColor];
  216. [self.view addSubview:self.navigationView];
  217. UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, JX_SCREEN_TOP - 40, JX_SCREEN_WIDTH, 40)];
  218. titleLabel.text = @"每日答题";
  219. titleLabel.textColor = [UIColor blackColor];
  220. titleLabel.font = [UIFont systemFontOfSize:18.0f];
  221. titleLabel.textAlignment = NSTextAlignmentCenter;
  222. [self.navigationView addSubview:titleLabel];
  223. self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
  224. self.backButton.frame = CGRectMake(15, 30, 30, 30);
  225. if(JX_SCREEN_HEIGHT >= 812)
  226. {
  227. self.backButton.frame = CGRectMake(15, 45, 30, 30);
  228. }
  229. [self.backButton setImage:[UIImage imageNamed:@"title_back_black"] forState:UIControlStateNormal];
  230. [self.backButton addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  231. [self.navigationView addSubview:self.backButton];
  232. }
  233. -(void)initComponents
  234. {
  235. self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.navigationView.frame.origin.y + self.navigationView.frame.size.height, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT - (self.navigationView.frame.origin.y + self.navigationView.frame.size.height))];
  236. self.scrollView.backgroundColor = self.view.backgroundColor;
  237. self.scrollView.contentSize = CGSizeMake(JX_SCREEN_WIDTH, self.scrollView.frame.size.height);
  238. [self.view addSubview:self.scrollView];
  239. UIImageView * backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 0.68*JX_SCREEN_WIDTH)];
  240. backView.image = [UIImage imageNamed:@"questionBacks"];
  241. [self.scrollView addSubview:backView];
  242. backView.userInteractionEnabled = YES;
  243. UIButton * postButton = [UIButton buttonWithType:UIButtonTypeCustom];
  244. postButton.frame = CGRectMake(JX_SCREEN_WIDTH - 70 - 15, backView.frame.size.height - 30, 70, 30);
  245. [postButton setBackgroundColor:[UIColor colorWithRed:253/255.0f green:219/255.0f blue:173/255.0f alpha:1.0f]];
  246. [postButton setTitle:@"提交" forState:UIControlStateNormal];
  247. [postButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  248. [postButton.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
  249. [postButton.layer setCornerRadius:5.0f];
  250. [postButton.layer setMasksToBounds:YES];
  251. [postButton addTarget:self action:@selector(postButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  252. [backView addSubview:postButton];
  253. // if(!self.ifAnswer.boolValue)
  254. // {
  255. // postButton.userInteractionEnabled = NO;
  256. // }
  257. // else
  258. // {
  259. // postButton.userInteractionEnabled = YES;
  260. // }
  261. UIImageView * questionView = [[UIImageView alloc]initWithFrame:CGRectMake(10, backView.frame.origin.y + backView.frame.size.height + 15, JX_SCREEN_WIDTH - 20, 0.84*(JX_SCREEN_WIDTH-20-40))];
  262. questionView.image = [UIImage imageNamed:@"questionImage"];
  263. [self.scrollView addSubview:questionView];
  264. questionView.userInteractionEnabled = YES;
  265. self.questionTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, questionView.frame.size.width, 40)];
  266. self.questionTitle.text = @"";
  267. self.questionTitle.textColor = [UIColor colorWithRed:115/255.0f green:20/255.0f blue:5/255.0f alpha:1.0f];
  268. self.questionTitle.font = [UIFont boldSystemFontOfSize:15.0f];
  269. self.questionTitle.textAlignment = NSTextAlignmentCenter;
  270. [questionView addSubview:self.questionTitle];
  271. self.questionLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 40, questionView.frame.size.width - 30, 40)];
  272. self.questionLabel.text = @"";
  273. self.questionLabel.textColor = self.questionTitle.textColor;
  274. self.questionLabel.font = [UIFont systemFontOfSize:15.0f];
  275. [questionView addSubview:self.questionLabel];
  276. self.questionTable = [[UITableView alloc]initWithFrame:CGRectMake(0, self.questionLabel.frame.origin.y + self.questionLabel.frame.size.height, questionView.frame.size.width, questionView.frame.size.height - (self.questionLabel.frame.origin.y + self.questionLabel.frame.size.height)) style:UITableViewStylePlain];
  277. self.questionTable.dataSource = self;
  278. self.questionTable.delegate = self;
  279. self.questionTable.separatorStyle = UITableViewCellSeparatorStyleNone;
  280. self.questionTable.backgroundColor = [UIColor clearColor];
  281. [questionView addSubview:self.questionTable];
  282. if([self.ifAnswer isEqualToString:@"NO"])
  283. {
  284. self.questionTable.userInteractionEnabled = NO;
  285. }
  286. else
  287. {
  288. self.questionTable.userInteractionEnabled = YES;
  289. }
  290. UIButton * preButton = [UIButton buttonWithType:UIButtonTypeCustom];
  291. preButton.frame = CGRectMake(JX_SCREEN_WIDTH/2 - 80 - 50, questionView.frame.origin.y + questionView.frame.size.height + 15, 80, 30);
  292. [preButton setBackgroundColor:[UIColor colorWithRed:253/255.0f green:219/255.0f blue:173/255.0f alpha:1.0f]];
  293. [preButton setTitle:@"上一题" forState:UIControlStateNormal];
  294. [preButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  295. [preButton.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
  296. [preButton.layer setCornerRadius:5.0f];
  297. [preButton.layer setMasksToBounds:YES];
  298. [preButton addTarget:self action:@selector(preButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  299. preButton.alpha =1.0f;
  300. [self.scrollView addSubview:preButton];
  301. UIButton * nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
  302. nextButton.frame = CGRectMake(JX_SCREEN_WIDTH/2 + 50, preButton.frame.origin.y, 80, 30);
  303. [nextButton setBackgroundColor:[UIColor colorWithRed:253/255.0f green:219/255.0f blue:173/255.0f alpha:1.0f]];
  304. [nextButton setTitle:@"下一题" forState:UIControlStateNormal];
  305. [nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  306. [nextButton.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
  307. [nextButton.layer setCornerRadius:5.0f];
  308. [nextButton.layer setMasksToBounds:YES];
  309. [nextButton addTarget:self action:@selector(nextButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
  310. nextButton.alpha = 1.0f;
  311. [self.scrollView addSubview:nextButton];
  312. self.scrollView.contentSize = CGSizeMake(JX_SCREEN_WIDTH, preButton.frame.origin.y + preButton.frame.size.height + 20);
  313. }
  314. -(void)nextButtonClicked:(UIButton *)sender
  315. {
  316. self.currentSort ++;
  317. BOOL check = NO;
  318. for(NSDictionary * dictionary in self.questionArray)
  319. {
  320. NSString * questionSort = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"sort"]];
  321. if(questionSort.intValue == self.currentSort)
  322. {
  323. NSString * questionTitle = [NSString stringWithFormat:@"第%@题:%@",questionSort,[dictionary objectForKey:@"title"]];
  324. NSString * questionDesc = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"desc"]];
  325. if(questionTitle != nil && questionTitle.length != 0 && ![questionTitle isEqualToString:@"(null)"] && ![questionTitle isEqualToString:@"<null>"])
  326. {
  327. self.questionTitle.text = questionTitle;
  328. }
  329. if(questionDesc != nil && questionDesc.length != 0 && ![questionDesc isEqualToString:@"(null)"] && ![questionDesc isEqualToString:@"<null>"])
  330. {
  331. self.questionLabel.text = questionDesc;
  332. }
  333. self.currentQuestion = dictionary;
  334. check = YES;
  335. break;
  336. }
  337. }
  338. if(!check)
  339. {
  340. UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前已是最后一题了" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
  341. [alert show];
  342. self.currentSort --;
  343. }
  344. else
  345. {
  346. [self.questionTable reloadData];
  347. }
  348. }
  349. -(void)preButtonClicked:(UIButton *)sender
  350. {
  351. if(self.currentSort > 1)
  352. {
  353. self.currentSort --;
  354. for(NSDictionary * dictionary in self.questionArray)
  355. {
  356. NSString * questionSort = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"sort"]];
  357. if(questionSort.intValue == self.currentSort)
  358. {
  359. NSString * questionTitle = [NSString stringWithFormat:@"第%@题:%@",questionSort,[dictionary objectForKey:@"title"]];
  360. NSString * questionDesc = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"desc"]];
  361. if(questionTitle != nil && questionTitle.length != 0 && ![questionTitle isEqualToString:@"(null)"] && ![questionTitle isEqualToString:@"<null>"])
  362. {
  363. self.questionTitle.text = questionTitle;
  364. }
  365. if(questionDesc != nil && questionDesc.length != 0 && ![questionDesc isEqualToString:@"(null)"] && ![questionDesc isEqualToString:@"<null>"])
  366. {
  367. self.questionLabel.text = questionDesc;
  368. }
  369. self.currentQuestion = dictionary;
  370. break;
  371. }
  372. }
  373. [self.questionTable reloadData];
  374. }
  375. else
  376. {
  377. UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"当前是第一题了" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
  378. [alert show];
  379. }
  380. }
  381. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  382. {
  383. NSArray * options = [self.currentQuestion objectForKey:@"options"];
  384. return options.count;
  385. }
  386. #pragma mark --- 代理 tableView
  387. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  388. {
  389. JXQuestionTableViewCell * cell = [JXQuestionTableViewCell cellWithTableView:tableView];
  390. NSArray * options = [_currentQuestion objectForKey:@"options"];
  391. NSDictionary * dictionary = [options objectAtIndex:[indexPath row]];
  392. NSString * lastAnswer = [NSString stringWithFormat:@"%@",[_currentQuestion objectForKey:@"answerid"]];
  393. if(lastAnswer != nil && lastAnswer.length != 0 && ![lastAnswer isEqualToString:@"(null)"] && ![lastAnswer isEqualToString:@"<null>"])
  394. {
  395. NSString * currentAnswerID = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"id"]];
  396. if(currentAnswerID.intValue == lastAnswer.intValue)
  397. {
  398. NSString * ifRight = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"right"]];
  399. if(ifRight.boolValue)
  400. {
  401. if (_indexRightRow==indexPath.row) {
  402. cell.correctImage.alpha = 1.0f;
  403. cell.greenGouIMG.alpha=1.0;
  404. }else{
  405. cell.correctImage.alpha = 1.0f;
  406. cell.greenGouIMG.alpha=1.0;
  407. }
  408. }else{
  409. cell.wrongImage.alpha =1.0f;
  410. cell.redGouIMG.alpha=1.0;
  411. [options enumerateObjectsUsingBlock:^(NSDictionary *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  412. NSLog(@"right:--%@",obj[@"right"]);
  413. if ([obj[@"right"] intValue]==1) { *stop=YES;
  414. _indexRightRow=idx;
  415. NSIndexPath *indexPath=[NSIndexPath indexPathForRow:idx inSection:0];
  416. [_questionTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  417. }else{
  418. cell.correctImage.alpha = 0.0f;
  419. cell.greenGouIMG.alpha=0.0;
  420. }
  421. }];
  422. }
  423. }else{
  424. }
  425. cell.userInteractionEnabled = NO;
  426. }
  427. else
  428. {
  429. cell.correctImage.alpha = 0.0f;
  430. cell.greenGouIMG.alpha=0.0;
  431. cell.wrongImage.alpha =0.0f;
  432. cell.redGouIMG.alpha=0.0;
  433. cell.userInteractionEnabled = YES;
  434. }
  435. CGFloat ansWidth=[self ZFYtextHeightFromTextString:[dictionary objectForKey:@"title"] width:300 fontSize:15].width;
  436. cell.answerLabel.text = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"title"]];
  437. cell.answerLabel.frame=CGRectMake(CGRectGetMaxX(cell.boxImage.frame)+15,0, ansWidth, 30);
  438. cell.wrongImage.frame=CGRectMake(CGRectGetMaxX(cell.answerLabel.frame)+15, 5, 20, 20);
  439. cell.correctImage.frame=CGRectMake(CGRectGetMaxX(cell.answerLabel.frame)+15, 5, 20, 20);
  440. cell.backgroundColor = [UIColor clearColor];
  441. cell.answerLabel.text = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"title"]];
  442. cell.backgroundColor = [UIColor clearColor];
  443. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  444. return cell;
  445. }
  446. -(CGSize)ZFYtextHeightFromTextString:(NSString *)text width:(CGFloat)textWidth fontSize:(CGFloat)size{
  447. //计算 label需要的宽度和高度
  448. NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:size]};
  449. CGSize size2 = [text boundingRectWithSize:CGSizeMake(textWidth, MAXFLOAT) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
  450. //CGSize size1 = [text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:size]}];
  451. return size2;
  452. }
  453. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  454. {
  455. return 40.0f;
  456. }
  457. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  458. {
  459. NSArray * options = [self.currentQuestion objectForKey:@"options"];
  460. NSDictionary * dictionary = [options objectAtIndex:[indexPath row]];
  461. NSString * answerID = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"id"]];
  462. NSMutableDictionary * mutableDic = self.currentQuestion.mutableCopy;
  463. [mutableDic setObject:answerID forKey:@"answerid"];
  464. self.currentQuestion = mutableDic.copy;
  465. //sort 代表第几题
  466. NSString * sort = [NSString stringWithFormat:@"%@",[self.currentQuestion objectForKey:@"sort"]];
  467. NSMutableArray * mutableArray = self.questionArray.mutableCopy;
  468. for(int i=0;i<=(self.questionArray.count - 1);i++)
  469. {
  470. NSDictionary * dict = [self.questionArray objectAtIndex:i];
  471. NSString * realSortID = [NSString stringWithFormat:@"%@",[dict objectForKey:@"sort"]];
  472. if(sort.intValue == realSortID.intValue)
  473. {
  474. [mutableArray replaceObjectAtIndex:i withObject:self.currentQuestion];
  475. break;
  476. }
  477. }
  478. self.questionArray = mutableArray;
  479. [_questionTable reloadData];
  480. NSString * ifRight = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"right"]];
  481. //return;
  482. if(ifRight.boolValue)
  483. {
  484. return;
  485. // self.alertLabel.text = @"恭喜你回答正确";
  486. if(self.currentSort != self.questionArray.count)
  487. {
  488. self.currentSort ++;
  489. for(NSDictionary * dictionary in self.questionArray)
  490. {
  491. NSString * questionSort = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"sort"]];
  492. if(questionSort.intValue == self.currentSort)
  493. {
  494. NSString * questionTitle = [NSString stringWithFormat:@"第%@题:%@",questionSort,[dictionary objectForKey:@"title"]];
  495. NSString * questionDesc = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"desc"]];
  496. if(questionTitle != nil && questionTitle.length != 0 && ![questionTitle isEqualToString:@"(null)"] && ![questionTitle isEqualToString:@"<null>"])
  497. {
  498. self.questionTitle.text = questionTitle;
  499. }
  500. if(questionDesc != nil && questionDesc.length != 0 && ![questionDesc isEqualToString:@"(null)"] && ![questionDesc isEqualToString:@"<null>"])
  501. {
  502. self.questionLabel.text = questionDesc;
  503. }
  504. self.currentQuestion = dictionary;
  505. break;
  506. }
  507. }
  508. //[self.questionTable reloadData];
  509. }
  510. }
  511. else
  512. {
  513. return;;
  514. //self.alertLabel.text = [NSString stringWithFormat:@"回答错误,正确答案是:%@",correctAnswer];
  515. if(self.currentSort != self.questionArray.count)
  516. {
  517. self.currentSort ++;
  518. for(NSDictionary * dictionary in self.questionArray)
  519. {
  520. NSString * questionSort = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"sort"]];
  521. if(questionSort.intValue == self.currentSort)
  522. {
  523. NSString * questionTitle = [NSString stringWithFormat:@"第%@题:%@",questionSort,[dictionary objectForKey:@"title"]];
  524. NSString * questionDesc = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"desc"]];
  525. if(questionTitle != nil && questionTitle.length != 0 && ![questionTitle isEqualToString:@"(null)"] && ![questionTitle isEqualToString:@"<null>"])
  526. {
  527. self.questionTitle.text = questionTitle;
  528. }
  529. if(questionDesc != nil && questionDesc.length != 0 && ![questionDesc isEqualToString:@"(null)"] && ![questionDesc isEqualToString:@"<null>"])
  530. {
  531. self.questionLabel.text = questionDesc;
  532. }
  533. self.currentQuestion = dictionary;
  534. break;
  535. }
  536. }
  537. // [self.questionTable reloadData];
  538. }
  539. }
  540. }
  541. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  542. {
  543. if(alertView.tag == 1000)
  544. {
  545. [g_navigation popToViewController:[JXApplyViewController class] animated:YES];
  546. }
  547. else if(alertView.tag == 10000)
  548. {
  549. self.currentSort ++;
  550. for(NSDictionary * dictionary in self.questionArray)
  551. {
  552. NSString * questionSort = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"sort"]];
  553. if(questionSort.intValue == self.currentSort)
  554. {
  555. NSString * questionTitle = [NSString stringWithFormat:@"第%@题:%@",questionSort,[dictionary objectForKey:@"title"]];
  556. NSString * questionDesc = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"desc"]];
  557. if(questionTitle != nil && questionTitle.length != 0 && ![questionTitle isEqualToString:@"(null)"] && ![questionTitle isEqualToString:@"<null>"])
  558. {
  559. self.questionTitle.text = questionTitle;
  560. }
  561. if(questionDesc != nil && questionDesc.length != 0 && ![questionDesc isEqualToString:@"(null)"] && ![questionDesc isEqualToString:@"<null>"])
  562. {
  563. self.questionLabel.text = questionDesc;
  564. }
  565. self.currentQuestion = dictionary;
  566. break;
  567. }
  568. }
  569. [self.questionTable reloadData];
  570. }
  571. }
  572. @end