struct ProductGridItem: View {
let product: AppleProduct
init(product: AppleProduct) {
self.product = product
print("init \(product.name)")
}
var body: some View {
VStack(alignment: .leading) {
Text(product.category)
.font(.subheadline)
.foregroundColor(.secondary)
Text(product.name)
.font(.title2)
Text(product.summary)
.font(.caption)
.multilineTextAlignment(.leading)
.frame(maxWidth: .infinity, alignment: .leading)
.padding()
.background(.gray.opacity(0.1), in: RoundedRectangle(cornerRadius: 10))
Spacer()
}
}
}
struct Grid: View {
var items = AppleProduct.sampleList
var body: some View {
VStack {
ForEach(0 ..< 3) { row in
HStack {
ForEach(0 ..< 2) { col in
ProductGridItem(product: items[row * 3 + col])
}
}
}
}
.padding()
}
}
다만 iOS 14부터 그리드 추가로 이렇게 하지 않아도 된다.