gpt4 book ai didi

ios - TableView 行上的开关更改会影响其他行

转载 作者:行者123 更新时间:2023-11-30 13:30:12 24 4
gpt4 key购买 nike

我有以下表格 View 单元格类:

class QuestionCell: UITableViewCell {
@IBOutlet weak var questionAnswer: UISwitch!
@IBOutlet weak var questionLabel: UILabel!

var data: Answer?

func configureForQuestion(data: Answer) {
print("configureForQuestion triggered")
questionLabel.text = data.question
self.data = data
}

@IBAction func questionAnswerChanged(sender: UISwitch) {
data?.answer = sender.on
}
}

该单元由标签和开关组成。目前,当我更改第一行的开关状态时,最后一行的开关状态也会更改。似乎没有其他行以这种方式连接。我对这个问题很困惑。

额外信息:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return answers.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(TableView.CellIdentifiers.QuestionCell, forIndexPath: indexPath) as! QuestionCell
cell.configureForQuestion(answers[indexPath.row])
return cell
}

最佳答案

您需要覆盖单元格中的prepareForReuse 并重置您的ui 元素数据。

override func prepareForReuse() {
self.questionLabel.text = nil
self.questionAnswer.on = data?.answer
}

关于ios - TableView 行上的开关更改会影响其他行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36650024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com