Я знаю, что есть много вопросов, похожих на мои, однако многие из них устарели, а другие не работали с моим делом.
У меня есть анкета в UITableView
, которая содержит 5 вопросов и 3 варианта для каждого.Выбор - это UIButton, который меняет изображение при нажатии.НО, когда ответят на первые вопросы, на пятый вопрос также отвечают!
Вот мои tableView
методы:
var questions: [Question] = []
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let question = questions[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "choicesCell") as! ChoicesCell
cell.setQuestion(question: question)
cell.selectionStyle = .none
return cell
}
Вот как я настраиваю кнопки выбора изображений, когдаони выбраны:
@IBAction func answerOneTapped(_ sender: UIButton) {
delegate?.didTapAnswerOne()
print("answerOneTapped")
answerOneButton.setImage(#imageLiteral(resourceName: "ElipseSelected"), for: .normal)
answerTwoButton.setImage(#imageLiteral(resourceName: "Elipse"), for: .normal)
answerThreeButton.setImage(#imageLiteral(resourceName: "Elipse"), for: .normal)
}
@IBAction func answerTwoTapped(_ sender: UIButton) {
delegate?.didTapAnswerTwo()
print("answerTwoTapped")
answerOneButton.setImage(#imageLiteral(resourceName: "Elipse"), for: .normal)
answerTwoButton.setImage(#imageLiteral(resourceName: "ElipseSelected"), for: .normal)
answerThreeButton.setImage(#imageLiteral(resourceName: "Elipse"), for: .normal)
}
@IBAction func answerThreeTapped(_ sender: UIButton) {
delegate?.didTapAnswerThree()
print("answerThreeTapped")
answerOneButton.setImage(#imageLiteral(resourceName: "Elipse"), for: .normal)
answerTwoButton.setImage(#imageLiteral(resourceName: "Elipse"), for: .normal)
answerThreeButton.setImage(#imageLiteral(resourceName: "ElipseSelected"), for: .normal)
}
Я попытался несколько решений без удачи, как:
questions.removeAll()
в cellForRowAt
и в ViewWillAppear
Я также пыталсяустановить кнопки на невыбранный выбор в:
override func prepareForReuse() {
super.prepareForReuse()
answerOneButton.setImage(#imageLiteral(resourceName: "Elipse"), for: .normal)
answerTwoButton.setImage(#imageLiteral(resourceName: "Elipse"), for: .normal)
answerThreeButton.setImage(#imageLiteral(resourceName: "Elipse"), for: .normal)
}
Я уже потратил много часов, пытаясь решить эту проблему.и я прочитал документы Apple относительно dequeueReusableCell
и собрал много информации безрезультатно ... Пожалуйста, помогите!