Reputation: 217
As I'm fiddling with SwiftUI a bit I'm wondering how to have a horizontal ScrollView
with 2 alternating rows.
I get the following message when using my example:
The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
import SwiftUI
struct LabelFilter: View {
var allLabels = ["one", "two", "three", "four", "five", "six", "seven"]
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
VStack {
HStack {
ForEach(allLabels.indices) { index in
if (index % 2 == 0) {
LabelButton(label: self.allLabels[index]) { }
}
}
}.padding()
HStack {
ForEach(allLabels.indices) { index in
if (index % 2 == 1) {
LabelButton(label: self.allLabels[index]) { }
}
}
}.padding()
}
}
}
}
Why is this happening, how to fix it and is there a better solution to it?
Upvotes: 1
Views: 333
Reputation: 2866
This code compiles:
struct LabelFilter: View {
var allLabels = ["one", "two", "three", "four", "five", "six", "seven"]
var evenLabels: [String] {
stride(from: 0, to: allLabels.count, by: 2).map { allLabels[$0] }
}
var oddLabels: [String] {
stride(from: 1, to: allLabels.count, by: 2).map { allLabels[$0] }
}
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
VStack {
HStack {
ForEach(oddLabels, id: \.self) { label in
Text(label)
}
}.padding()
HStack {
ForEach(evenLabels, id: \.self) { label in
Text(label)
}
}.padding()
}
}
}
}
Upvotes: 1