我有一个基本测验,其中 uitableview 呈现四个单元格。三个单元格有错误的答案,一个有正确的答案。我已经包含了警报,所以这会告诉用户他/她哪里不正确,哪里正确。
我还尝试包含一个触觉功能,但是当我在 iPhone 7 上运行该应用程序时,尽管 x 代码没有错误,但触觉功能不起作用。
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)
}