I have this quiz application where the user is presented with a question and four options. The options are within a table view function and the label is changed via code for each question. I would like to add image based questions. I have some images in my asset but when I try
image.UIImageView this is an error.
I would like it so that when the new question and answers are called up by the user, a new image is presented in the image view.
Here is the code, as well as a provided screenshot of the view controller.
import UIKit
class imagequizpage1: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var label: UILabel!
@IBOutlet var image: UIImageView!
@IBOutlet var table: UITableView!
var gameModels = [Question]()
var currentQuestion: Question?
override func viewDidLoad() {
super.viewDidLoad()
table.delegate = self
table.dataSource = self
setupQuestions()
configureUI(question: gameModels.first!)
}
private func configureUI(question: Question) {
label.text = question.text
currentQuestion = question
table.reloadData()
}
private func checkAnswer(answer: Answer, question: Question) -> Bool{
return question.answers.contains(where: { $0.text == answer.text }) && answer.correct
}
private func setupQuestions(){
gameModels.append(Question(text: "What is this image", answers: [
Answer(text: "A bridge", correct: true),
Answer(text: "A waterway.", correct: false),
Answer(text: "A skyline", correct: false),
Answer(text: "A park", correct: false)
]))
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentQuestion?.answers.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = currentQuestion?.answers[indexPath.row].text
cell.textLabel?.numberOfLines = 0
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
guard let question = currentQuestion else {
return
}
let answer = question.answers[indexPath.row]
if checkAnswer(answer: answer, question: question) {
// correct
if let index = gameModels.firstIndex(where: { $0.text == question.text }) {
if index < (gameModels.count - 1){
// next question
let nextQuestion = gameModels[index + 1]
currentQuestion = nil
configureUI(question: nextQuestion)
}
else{
let alert = UIAlertController(title: "Done", message: "You beat the game" , preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
present(alert, animated: true)
}
}
}
else{
let alert = UIAlertController(title: "Wrong", message: "That's wrong", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
present(alert, animated: true)
}
}
struct Question {
let text: String
let answers: [Answer]
}
struct Answer {
let text: String
let correct: Bool
}
}
[
Many Thanks.
You need to set image to imageView
imageView.image = UIImage(named: "Asset Name")
Note: change your UIImageView
name ... instead of image
use imageView