我通过创建一个新的空白项目发现了一个问题,列表中最多只有 10 个文本元素,否则会导致编译错误,我可以在任何地方编辑限制吗?
struct ContentView: View {
var body: some View {
List {
Text("Line 1")
Text("Line 2")
Text("Line 3")
Text("Line 4")
Text("Line 5")
Text("Line 6")
Text("Line 7")
Text("Line 8")
Text("Line 9")
Text("Line 10")
// Uncomment this line will cause Complie Error
// Text("Line 11")
}
}
}
我尝试添加模型
class MyModel: ObservableObject {
@Published var items: [String] = [
"line 1",
"line 2",
"line 3",
"line 4",
"line 5",
"line 6",
"line 7",
"line 8",
"line 9",
"line 10",
"line 11",
]
}
并按模型数据显示列表,显示更正
@ObservedObject var model = MyModel()
var body: some View {
List {
ForEach(model.items.indices) { index in
Text(self.model.items[index])
}
}
}
那么,问题不在于 List,是 Function Builder 吗?
更新了一个新方法,使用Group,对于VStack,是可以的,但是对于List,它会显示11行,并且每一行都与“Line 11”重叠
List {
Group {
Text("Line 1")
Text("Line 2")
Text("Line 3")
Text("Line 4")
Text("Line 5")
Text("Line 6")
Text("Line 7")
Text("Line 8")
Text("Line 9")
Text("Line 10")
}
Group {
Text("Line 11")
}
}