英文:
SwiftUI: How to make a list that ignores the max of HStacks
问题
以下是您要翻译的内容:
"I would like to create a list like this
But as soon as I add more than 10 elements I get the error
"Extra argument in call"
I know this error happens because I have too many Stacks in my View
What would be the best way to build a list like this that is scrollable and I can add Views with different values that are stylized?
My code so far
List{
CardView()
CardView()
CardView()
CardView()
CardView()
}
And the CardView is
struct CardView: View {
var body: some View {
HStack{
Image(systemName: "circle.hexagongrid")
Divider()
Text("Text")
.frame(minWidth: 100)
Divider()
Spacer()
Text("1111")
}
}
}
Is there a better option in SwiftUI to display lists like this?
I would also like to have a bit of space between the CardViews, but I can figure that out later when I know how to build my list."
英文:
I would like to create a list like this
But as soon as I add more then 10 elements I get the error
"Extra argument in call"
I know this error happens because I have to many Stacks in my View
What would the best way be to build a list like this that is scrollable and I can add Views With different values that are stylized?
my code so far
List{
CardView()
CardView()
CardView()
CardView()
CardView()
}
and the CardView is
struct CardView: View {
var body: some View {
HStack{
Image(systemName: "circle.hexagongrid")
Divider()
Text("Text")
.frame(minWidth: 100)
Divider()
Spacer()
Text("1111")
}
}
}
is there a better option in SwiftUI to display lists like this?
I would also like to have a bit of space between the CardView's but I can figure that out later when I know how to build my list.
答案1
得分: 1
你可以通过使用 ForEach
来实现这个功能。
struct ContentView: View {
var body: some View {
List {
ForEach(0..<30) { item in
CardView()
}
}
}
}
30 是将要创建的视图的数量。
希望这有所帮助
英文:
You can achieve this by using ForEach
struct ContentView: View {
var body: some View {
List {
ForEach(0..<30) { item in
CardView()
}
}
}
}
30 is the number of views that will get created
I hope this helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论