【发布时间】:2023-04-05 15:11:01
【问题描述】:
我正在尝试创建问题列表。
我计划为每个问题创建一个“部分”,并根据类型更改每一行
现在我有很多不同类型的问题。
比如说:
- 询问一些文字输入
- 从选择器中选择
- 多选(因此只需显示所有选项)
我有这种设置在“常规”iOS 中工作 但是,当尝试在 SwiftUI 中实现这样的事情时,预览不断释放,我似乎也无法让构建工作。我真的没有从 xcode 得到任何反馈。
示例代码:
import SwiftUI
struct Question: Hashable, Codable, Identifiable {
var id: Int
var label: String
var type: Int
var options: [Option]?
var date: Date? = nil
}
struct Option : Hashable, Codable, Identifiable {
var id: Int
var value: String
}
struct MyList: View {
var questions: [Question] = [
Question(id: 1, label: "My first question", type: 0),
Question(id: 2, label: "My other question", type: 1, options: [Option(id: 15, value: "Yes"), Option(id: 22, value: "No")]),
Question(id: 3, label: "My last question", type: 2, options: [Option(id: 4, value: "Red"), Option(id: 5, value: "Green"), Option(id: 6, value: "Blue")])
]
var body: some View {
List {
ForEach(questions) { question in
Section(header: Text(question.label)) {
if(question.type == 0)
{
Text("type 0")
//Show text entry row
}
else if(question.type == 1)
{
Text("type 1")
//Show a picker containing all options
}
else
{
Text("type 2")
//Show rows for multiple select
//
// IF YOU UNCOMMENT THIS, IT STARTS FREEZING
//
// if let options = question.options {
// ForEach(options) { option in
// Text(option.value)
// }
// }
}
}
}
}
}
}
struct MyList_Previews: PreviewProvider {
static var previews: some View {
MyList()
}
}
在 SwiftUI 中这样的事情可能吗? 我错过了什么? 为什么会结冰?
【问题讨论】:
标签: ios swift swiftui swiftui-list