1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| class OnlineTestDetailVC: CommentTableViewVC { var currentAnwserCell:OnlineTestDetailCell! var arrayData:[OnlineTestDetailModel] = [] private var answerIndex:Int = 0 private var choose_answer:Int = 0 private var current_cell:OnlineAnswerCell? var questionID:Int = 0 override func viewDidLoad() { super.viewDidLoad() self.navigationItem.title = "在线测试" tableView.delegate = self tableView.dataSource = self tableView.mj_header = nil registCell() request_question_list() } func request_question_list(){ showStaus("正在请求...") NetworkTool.requestData(urlString: api_start_testing,params: ["classify":questionID], method: .post) { (res) in if let model = OnlineTestArrayDetailModel.deserialize(from: res){ guard let array = model.data else { return } self.arrayData = array self.tableView.reloadData() } } } func registCell(){ tableView.register(UINib(nibName:"OnlineTestDetailCell",bundle:nil), forCellReuseIdentifier: "cell") tableView.register(UINib(nibName:"LastAndNextCell",bundle:nil), forCellReuseIdentifier: "cell2") tableView.register(UINib(nibName:"OnlineAnswerCell",bundle:nil), forCellReuseIdentifier: "cell3") } func request_submit_finish(){ showStaus("请稍后...") let qIDs = self.arrayData.flatMap{ $0.id } let answers = self.arrayData.flatMap{ $0.choose_answer } NetworkTool.requestData(urlString: api_submit_an_answer,params: ["uid":UserMananager.manager.uid ?? "","question":qIDs,"answer":answers,"classify":questionID], method: .post) { (res) in if let code = res["code"] as? Int{ if code == 200 { if let model = OnlineTestRecordModel.deserialize(from: res["info"] as? [String:Any]){ let vc = OnlineTestRecordDetailVC() vc.answerModel = model vc.arrayQuestionData = self.arrayData self.push_vc(sender: vc) } } } } } @objc func btn_next_action(sender:UIButton){ if sender.titleLabel?.text == "完成交卷" { request_submit_finish();return } let model = self.arrayData[answerIndex] if model.choose_answer == nil { return } if answerIndex + 1 >= self.arrayData.count { return } answerIndex = answerIndex + 1 self.tableView.reloadData() } @objc func btn_last_action(){ if answerIndex - 1 < 0 { return } answerIndex = answerIndex - 1 self.tableView.reloadData() } func check_answer(){ if current_cell == nil { return } let model = self.arrayData[answerIndex] model.choose_answer = choose_answer let right = Int(model.right ?? "0")! if model.choose_answer == right{ let cell = tableView.cellForRow(at: IndexPath(row: model.choose_answer!, section: 0)) as? OnlineAnswerCell cell?.labelAnswer.setImage(UIImage(named:"对勾"), for: .normal) }else{ let cell = tableView.cellForRow(at: IndexPath(row: model.choose_answer!, section: 0)) as? OnlineAnswerCell cell?.labelAnswer.setImage(UIImage(named:"错"), for: .normal) let cell2 = tableView.cellForRow(at: IndexPath(row: right, section: 0)) as? OnlineAnswerCell cell2?.labelAnswer.setImage(UIImage(named:"对勾"), for: .normal) } finish_changeBtn() } func finish_changeBtn(){ if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 1)) as? LastAndNextCell, answerIndex + 1 >= self.arrayData.count{ cell.btnlast.isHidden = true cell.btnNext.snp.remakeConstraints({ (make) in make.width.equalTo(150) make.height.equalTo(50) make.top.equalTo(50) make.centerX.equalToSuperview() }) cell.btnNext.setTitle("完成交卷", for: .normal) } } }
extension OnlineTestDetailVC:UITableViewDelegate,UITableViewDataSource{ func numberOfSections(in tableView: UITableView) -> Int { return 2 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section == 0 { if self.arrayData.count != 0 { let model = self.arrayData[answerIndex] let count = model.answers?.count ?? 0 return count + 1 } return 0 } return 1 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if indexPath.section == 1 { return 300 } if indexPath.row == 0 { let model = self.arrayData[answerIndex] let text = model.question ?? "" return text.get_heightForComment(fontSize: fontMid, width: SCREEN_WIDTH - 40) + 80 }else{ return 60 } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if indexPath.section == 1{ let cell = tableView.dequeueReusableCell(withIdentifier: "cell2") as? LastAndNextCell cell?.btnlast.addTarget(self, action: #selector(btn_last_action), for: .touchUpInside) cell?.btnNext.addTarget(self, action: #selector(btn_next_action(sender:)), for: .touchUpInside) return cell! }else{ let model = self.arrayData[answerIndex] if indexPath.row == 0 { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? OnlineTestDetailCell cell?.model = model cell?.labelIndex.text = "第\(answerIndex+1)题" return cell! } let cell = tableView.dequeueReusableCell(withIdentifier: "cell3") as? OnlineAnswerCell cell?.labelAnswer.setTitle(" \(model.answers![indexPath.row - 1])", for: .normal) if model.choose_answer == nil { cell?.labelAnswer.setImage(nil, for: .normal) }else{ let right = Int(model.right ?? "0")! if indexPath.row == right{ cell?.labelAnswer.setImage(UIImage(named:"对勾"), for: .normal) }else if indexPath.row == model.choose_answer{ cell?.labelAnswer.setImage(UIImage(named:"错"), for: .normal) }else{ cell?.labelAnswer.setImage(nil, for: .normal) } } return cell! } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let model = self.arrayData[answerIndex] if model.choose_answer != nil { return } if let cell = tableView.cellForRow(at: indexPath) as? OnlineAnswerCell{ self.choose_answer = indexPath.row self.current_cell = cell check_answer() }else{ self.current_cell = nil } } }
|