0
選択したセル内にイメージが表示され、セルが再度選択されるとこのイメージが消えるなど、セルがあるテーブルビューがあります。送信ボタンを押すと、選択したセルが記憶され、テーブルビューは新しいデータでリロードされます。しかし、これを行うと、新しいデータがすべて読み込まれますが、選択されたセルイメージはそのまま残ります。私はメインのキューでtableView.reloadData()を呼び出すことを試みましたが、それは依然として続きます。送信ボタンを数回押しても画像が残っています。テーブルビュー再ロード後のセルイメージの保存
HERESに私のコード:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentQuestion.answers.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.height/CGFloat(currentQuestion.answers.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
setSelectedArray()
let cell: AnswersTableViewCell = tableView.dequeueReusableCell(withIdentifier: "answersTableViewCell") as! AnswersTableViewCell
let text = currentQuestion.answers[indexPath.row]
let isAnAnswer = currentQuestion.answerKeys[indexPath.row]
cell.answerTextLabel.text = text
cell.answerView.backgroundColor = UIColor.white.withAlphaComponent(0.5)
cell.contentView.sendSubview(toBack: cell.answerView)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell: AnswersTableViewCell = tableView.cellForRow(at: indexPath) as? AnswersTableViewCell {
if cell.answerImageView.image == nil {
cell.answerImageView.image = UIImage(named: "paw.png")
selected[indexPath.row] = true
} else {
cell.answerImageView.image = nil
selected[indexPath.row] = false
}
}
}
@IBAction func submitButtonWasPressed() {
if questionNumber < questions.count - 1 {
questionNumber += 1
setCurrentQuestion()
self.answersTableView.reloadData()
self.view.setNeedsDisplay()
}
}
すべてのヘルプは素晴らしいことです。ありがとう
驚くべきことに、私はとても似ていますが失敗しました。ありがとう! – Wazza