-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResultsViewController.swift
67 lines (55 loc) · 2.01 KB
/
ResultsViewController.swift
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
//
// ResultsViewController.swift
// QuizApp
//
// Created by Joseph McCraw on 4/28/20.
// Copyright © 2020 Joseph McCraw. All rights reserved.
//
import Foundation
import UIKit
class ResultsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
private(set) var summary = ""
private(set) var answers = [PresentableAnswer]()
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var headerLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.rowHeight = UITableView.automaticDimension
headerLabel.text = summary
tableView.register(CorrectAnswerCell.self)
tableView.register(WrongAnswerCell.self)
}
convenience init(summary: String, answers: [PresentableAnswer] = []) {
self.init()
self.summary = summary
self.answers = answers
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
answers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let answer = answers[indexPath.row]
if answer.wrongAnswer == nil {
return correctAnswerCell(for: answer)
}
return wrongAnswerCell(for: answer)
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return answers[indexPath.row].wrongAnswer == nil ? 70 : 90
}
private func correctAnswerCell(for answer: PresentableAnswer) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(CorrectAnswerCell.self)!
cell.questionLabel?.text = answer.question
cell.answerLabel?.text = answer.answer
return cell
}
private func wrongAnswerCell(for answer: PresentableAnswer) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(WrongAnswerCell.self)!
cell.questionLabel?.text = answer.question
cell.correctAnswerLabel?.text = answer.answer
cell.wrongAnswerLabel?.text = answer.wrongAnswer
return cell
}
}