class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!
var questions:[Question] = []
var sectionCountGlobal = 0
override func viewDidLoad() {
super.viewDidLoad()
questions = fillQuestions()
}
func fillQuestions()-> [Question]{
var temp : [Question] = []
var choices : [Choice] = []
let choice = Choice(id: 1, text: "choice ", status: 1, questionId: 1)
choices.append(choice)
choices.append(choice)
choices.append(choice)
choices.append(choice)
choices.append(choice)
choices.append(choice)
let q1 = Question(id: 1, text: "Ahmad 55 years old man with a history of hypertension and hypocholesteremia was in a wedding and during the party he starts to feel chest pain and dizzy, his wife brought him to the emergency department. The ER nurse checked his vital signs: BP 88/50, HR: 45, RR:10, SPaO2: 90% and O2 per nasal cannula was started at 4l/minute. Few seconds later Mr.Ahmad lost consciousness and the code blue team were activated.", choices: choices)
let q2 = Question(id: 1, text: "question 2", choices: choices)
let q3 = Question(id: 1, text: "question 3", choices: choices)
temp.append(q1)
temp.append(q2)
temp.append(q3)
return temp
}
func numberOfSections(in tableView: UITableView) -> Int {
return questions.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
sectionCountGlobal = section
return questions[section].choices.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
let questionTextCell = tableView.dequeueReusableCell(withIdentifier: "QuestionTextCell") as! QuestionTextCell
questionTextCell.setQuestionText(text: questions[indexPath.section].text)
return questionTextCell
}else{
let choiceCell = tableView.dequeueReusableCell(withIdentifier: "ChoiceCell") as! ChoiceCell
choiceCell.choiceText.text = questions[indexPath.section].choices[indexPath.row].text
return choiceCell
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let questionNumber = "Q" + String(section+1)
return questionNumber
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 3
}
override func viewWillAppear(_ animated: Bool) {
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
}
我正在开发一个测验应用程序,每个问题都有多个选择,所以当检查一个单元格中的单选按钮并滚动到其他单元格时,我发现其他单元格在没有触及它们的情况下被检查了,解决方案是什么。
我尝试了不同的单元格重用方法,也尝试了 prepareForReuse() 但没有任何效果我如何独立处理每个单元格而不受其他单元格的影响,我不知道它来自服务器的问题数量。
最佳答案
在您的cellForRowAt
实现中,您必须根据单元格是否被选中来重置单元格的状态。由于单元格重用,您可以获得以前选择的单元格,但现在不应该选择 - 在这种情况下,您必须告诉单元格取消选择(反之亦然):
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
let questionTextCell = tableView.dequeueReusableCell(withIdentifier: "QuestionTextCell") as! QuestionTextCell
questionTextCell.setQuestionText(text: questions[indexPath.section].text)
return questionTextCell
} else {
let choiceCell = tableView.dequeueReusableCell(withIdentifier: "ChoiceCell") as! ChoiceCell
// here detect if the cell should be selected and set it accordingly, so something like:
let isSelected = isSelectedChoice(questions[indexPath.section].choices[indexPath.row])
choiceCell.isSelected = isSelected
// of course this is just a mockup, since I don't know exactly how you manage selection,
// but it should get you on the right path
choiceCell.choiceText.text = questions[indexPath.section].choices[indexPath.row].text
return choiceCell
}
}
关于ios - UITableView 中的单元格重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48769205/