How do I implement a collection view (that is, rectangles arranged in a grid) in SwiftUI?
I've tried ForEach-ing over a range of values, then using the counter as an index for an Array but have had numerous problems so are looking for a different way to achieve a collection view.
The cells to wrap over multiple rows. I have a SegmentedControl which is used to set how many cells to squeeze on one row.
Here is what I have:
VStack { // Multiple rows
ForEach((0..<requiredRows).identified(by: \.self)) { row in // Iterate over (previously calculated) required row count
HStack(spacing: 50)) { // Row
ForEach((0..<self.maximumOnXAxis).identified(by: \.self)) { cell in // Iterate over available cells
MyView(
width: calculatedSize,
height: calculatedSize / 2.0,
colour: Color(
red: lst[row * self.maximumOnXAxis][0],
green: lst[row * self.maximumOnXAxis][1],
blue: lst[row * self.maximumOnXAxis][2]
)
)
}
}
}
And all of the above is inside a ScrollView.
The current problem is a 'Could not type check in reasonable time ...', which happened after I add the colour: Colour(...) parameter to MyView.
