Ios UITableView中的单元重用
我正在开发一个测验应用程序,每个问题都有多个选项,因此当检查单元格中的单选按钮并滚动到其他单元格时,我发现其他单元格在未触摸它们的情况下进行了检查。解决方案是什么 我也尝试了不同的单元重用方法prepareForReuse(),但什么都不起作用。如何独立处理每个单元而不受其他单元的影响,我不知道服务器会提出多少问题Ios UITableView中的单元重用,ios,swift,uitableview,Ios,Swift,Uitableview,我正在开发一个测验应用程序,每个问题都有多个选项,因此当检查单元格中的单选按钮并滚动到其他单元格时,我发现其他单元格在未触摸它们的情况下进行了检查。解决方案是什么 我也尝试了不同的单元重用方法prepareForReuse(),但什么都不起作用。如何独立处理每个单元而不受其他单元的影响,我不知道服务器会提出多少问题 在您的cellForRowAt实现中,您必须根据是否选中单元格来重置单元格的状态。由于单元重复使用,您可以获得一个以前选定但现在不应选定的单元-在这种情况下,您必须告诉该单元取消选
在您的
cellForRowAt
实现中,您必须根据是否选中单元格来重置单元格的状态。由于单元重复使用,您可以获得一个以前选定但现在不应选定的单元-在这种情况下,您必须告诉该单元取消选定(反之亦然):
代码中的问题是您没有更改单选按钮的状态。从didSelectRowAt方法中选择选项时,必须更改所选选项的状态。根据您的选择模式,您可以更改特定选择状态的状态。以下两种方法都可以管理您的选择(您的状态变量应为Bool类型):
将您的代码包括在问题中(使用编辑按钮,不要将其放在注释中),特别是您的
cellForRowAt
实现这通常发生在您根据用户的操作更改UI时,而不是更改数据然后刷新UI时。如果看不到代码,就很难说得更具体。对不起,我刚刚编辑了这篇文章,现在你可以看到代码了
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
}
}
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
}
}
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
// update your radio button UI
choiceCell.radioButton.isSelected = questions[indexPath.section].choices[indexPath.row].status
choiceCell.choiceText.text = questions[indexPath.section].choices[indexPath.row].text
return choiceCell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
questions[indexPath.section].choices[indexPath.row].status = !questions[indexPath.section].choices[indexPath.row].status
tableView.reloadData()
}