cuốn sách gpt4 ai đã làm

ios - 如何在 swift 中将所选的问卷选择/答案保存为字符串?

In lại Tác giả: Walker 123 更新时间:2023-11-29 05:37:25 hai mươi bốn 4
mua khóa gpt4 Nike

我正在尝试使用 swift (tableview) 在 Xcode 中创建一份调查问卷,应用程序的用户可以选择他们的首选(例如,一个问题可以是,您有空吗?用户有一个选项列表可供选择并且可以选择多个)

为此,我创建了一个名为 potans(潜在答案的缩写)的变量和另一个名为 selectedans 的变量,它保存有关所选答案的信息。

当用户单击其中一个答案时,所选选项旁边会出现一个复选标记,并且还会打印所选答案的行号。当用户再次单击已选择的选项时,他们会自动取消选择该选项,并删除复选标记,并再次打印取消选择的行的行号。

我希望能够在用户按下“下一步按钮”时存储所选的答案。但是,由于用户可以改变主意(选择然后取消选择)并且可以选择多个答案,因此我不知道如何将行号信息转换为字符串名称。

因此,当用户按下(引用我编写的代码)ans1时,会出现一个复选标记并打印(输出)row:0。如果用户不执行任何其他操作并按下“下一步”按钮,则一切正常。但是,如果用户取消选择 ans1 (我们打印了另一行:0),然后选择了 ans2 和 ans3 (然后我们打印了 row:1 和 row:2),那么我需要添加哪行代码才能得出用户的最终答案是ans2和ans3?

我曾想过以某种方式使两个相同的行号(连续或不连续)抵消,但我不知道该怎么做。

var potans = ["ans1", "ans2", "ans3", "ans4", "ans5"]

var selectedans:[String] = []

//some irrelevant code here

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return potans.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = potans[indexPath.row]
let selectedView = UIView()
selectedView.backgroundColor = UIColor(red:0.65, green:0.85, blue:0.62, alpha:1.0)
cell.selectedBackgroundView = selectedView

return cell

}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

if tableView.cellForRow(at: indexPath as IndexPath)?.accessoryType == .checkmark
{
tableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .none
} khác {

tableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .checkmark
}

selectedans.append(potans[indexPath.row])

print("row: \(indexPath.row)")
}

//Created a 'next' button going to the next question

@objc func buttonAction(sender: UIButton!) {
self.performSegue(withIdentifier: "segue", sender: self)

}

现在,输出只是选择和取消选择的行号。实际上,我想要一个显示最终行号的输出,忽略选定的然后取消选定的答案。

câu trả lời hay nhất

Bạn có thể sử dụngUITableViewDelegatecủatableView(_:didDeselectRowAt:)方法并在该方法中删除selectedans数组中的相关数据。

关于ios - 如何在 swift 中将所选的问卷选择/答案保存为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56909002/

hai mươi bốn 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress