Swift - How to insert view after every x amount of items in foreach loop

40 Views Asked by At

I am trying to create a Foreach loop in a LazyVGrid where a full width view is shown on it's own row after a certain amount of items.

I'm currently having a hard time getting the view to show in the Foreach loop, I have to place it outside of the loop but then I'm unable to get the current index/indices. Using a Int for a counter didn't work either.

LazyVGrid(columns: gridItemLayout, spacing: 30) {
    ForEach(myArray.indices, id: \.self) { i in
        MyView()
        if i > 3 || i > 6 || i > 9 {
            MyOtherView()
        }
    }
}

Also trying to figure out how to make the counter dynamic so it works in multiples of 3s -

i > 3 || i > 6 || i > 9

Not sure how to go about getting the view to show in the Foreach loop on it's own row (in a LazyVGrid) after a certain amount of items, any suggestions is appreciated.

example img

0

There are 0 best solutions below