0

I have a basic quiz where the uitableview presents four cells. Three cells have wrong answers, and one has a correct answer. I already have included alerts, so this tells the user where he/she has been incorrect and where they are correct.

I have tried to also include a haptic function, however when I run the app on an iPhone 7, the haptic function does not work despite no errors from x code.

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
    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){
                func correct (_sender: UITableViewCell){
                    let generator = UINotificationFeedbackGenerator()
                    generator.notificationOccurred(.success)
                }
            // next question
                let nextQuestion = gameModels[index + 1]
                print("\(nextQuestion.text)")
                currentQuestion = nil
                configureUI(question: nextQuestion)
        }
        else{
                // end of game
                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 {
        //wrong
        let alert = UIAlertController(title: "Wrong", message: "Try again", preferredStyle: .alert)
        func wrongTap (_ sender: UITableViewCell){
            let generator = UINotificationFeedbackGenerator()
            generator.notificationOccurred(.error)

        }
        alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
        present(alert, animated: true)
    }
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Henry Hall
  • 39
  • 6

1 Answers1

0

Try to use impactAccurred instead:

            let generator = UIImpactFeedbackGenerator(style: .heavy)
            generator.prepare()
            generator.impactOccurred()
Vadim F.
  • 881
  • 9
  • 21