gpt4 book ai didi

ios - Swift 在 UITableViewCells 中保留 UISegmentedControl 值

转载 作者:行者123 更新时间:2023-11-28 10:07:44 25 4
gpt4 key购买 nike

我正在创建一个带有自定义单元格的测验应用程序,其中包含问题标签,然后是来自 UISegmentedControl 的答案。

分段控件的值在滚动时会发生变化,这会导致分数不准确。我知道这是因为 UITableView 重用了单元格。

我的主 vc 中的 tableview 数据源只是来自 plist 文件的所有问题的标签。

我的自定义 tableviewcell 类的代码是

class QuestionsTableViewCell: UITableViewCell {

@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var selection: UISegmentedControl!

var question: String = "" {
didSet {
if (question != oldValue) {
questionLabel.text = question
}
}
}

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

//Just for testing
@IBAction func segmentChanged(_ sender: UISegmentedControl) {
print("value is ", sender.selectedSegmentIndex);
}
}

View 存储在 .XIB 文件中的位置。

我的主 vc 的代码是

class ViewController: UIViewController, UITableViewDataSource {

let questionsTableIdentifier = "QuestionsTableIdentifier"
@IBOutlet var tableView:UITableView!

var questionsArray = [String]();

var questionsCellArray = [QuestionsTableViewCell]();

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let path = Bundle.main.path(forResource:
"Questions", ofType: "plist")
questionsArray = NSArray(contentsOfFile: path!) as! [String]

tableView.register(QuestionsTableViewCell.self,
forCellReuseIdentifier: questionsTableIdentifier)
let xib = UINib(nibName: "QuestionsTableViewCell", bundle: nil)
tableView.register(xib,
forCellReuseIdentifier: questionsTableIdentifier)
tableView.rowHeight = 108;
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(
withIdentifier: questionsTableIdentifier, for: indexPath)
as! QuestionsTableViewCell

let rowData = questionsArray[indexPath.row]
cell.question = rowData
return cell
}

@IBAction func calculate(_ sender: UIButton) {

var score = 0

for cell in tableView.visibleCells as! [QuestionsTableViewCell] {
score += cell.selection.selectedSegmentIndex
}

let msg = "Score is, \(score)"

print(msg)
}

@IBAction func reset(_ sender: UIButton) {
for cell in tableView.visibleCells as! [QuestionsTableViewCell] {
cell.selection.selectedSegmentIndex = 0;
}

}
}

我想做的只是跟踪数组中问题单元格的所有“选择”更改,然后在 cellForRowAt 中使用该数组。我只是对如何动态跟踪另一个类中的 View 的更改感到困惑。我是 Swift 的新手,想解决这是一种合适的 MVC 方式。谢谢

最佳答案

创建一个包含文本和选定索引的,而不是简单的字符串数组作为数据源

class Question {
let text : String
var answerIndex : Int

init(text : String, answerIndex : Int = 0) {
self.text = text
self.answerIndex = answerIndex
}
}

questionArray声明为

var questions = [Question]()

填充 viewDidLoad中的数组
let url = Bundle.main.url(forResource: "Questions", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let questionsArray = try! PropertyListSerialization.propertyList(from: data, format: nil) as! [String]
questions = questionsArray.map {Question(text: $0)}

在自定义单元格中添加回调并在传递所选索引的segmentChanged 方法中调用它,不需要属性question,标签在中更新> Controller 的cellForRow

class QuestionsTableViewCell: UITableViewCell {

@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var selection: UISegmentedControl!

var callback : ((Int) -> ())?

@IBAction func segmentChanged(_ sender: UISegmentedControl) {
print("value is ", sender.selectedSegmentIndex)
callback?(sender.selectedSegmentIndex)
}
}

cellForRow中添加回调并在闭包中更新模型

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: questionsTableIdentifier, for: indexPath) as! QuestionsTableViewCell

let question = questions[indexPath.row]
cell.questionLabel.text = question.text
cell.selection.selectedSegmentIndex = question.answerIndex
cell.callback = { index in
question.answerIndex = index
}
return cell
}

要重置单元格中的分段控件,请将模型中的属性设置为 0 并重新加载 TableView

@IBAction func reset(_ sender: UIButton) {
questions.forEach { $0.answerIndex = 0 }
self.tableView.reloadData()
}

现在您可以直接从模型而不是 View 计算分数。

关于ios - Swift 在 UITableViewCells 中保留 UISegmentedControl 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51574546/

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