Search code examples
swiftuiswiftui-list

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 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

list Image

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.


Solution

  • 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 :)