質問編集履歴
2
タグの追加
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
1
ソースの追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -39,4 +39,64 @@
|
|
39
39
|
==============================
|
40
40
|
「上部タブ内のページを切り替えようと指を動かしたら表示された時の画像」
|
41
41
|
果物が一列に横並びになっている部分が1ページ目表示されていなかった部分です。
|
42
|
-

|
42
|
+

|
43
|
+
|
44
|
+
表示画面のビューとデーター取得のソースは以下になります。
|
45
|
+
```ここに言語を入力
|
46
|
+
struct ProductListView: View {
|
47
|
+
|
48
|
+
@ObservedObject var productVM = ProductViewModel()
|
49
|
+
var body: some View {
|
50
|
+
|
51
|
+
let chunkedProducts = self.productVM.products.chunked(into: 3)
|
52
|
+
|
53
|
+
ScrollView {
|
54
|
+
|
55
|
+
VStack {
|
56
|
+
ForEach(0..<chunkedProducts.count) { index in
|
57
|
+
HStack {
|
58
|
+
ForEach(chunkedProducts[index]) { product in
|
59
|
+
ProductCell(product: product)
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
ーーーー
|
69
|
+
import Foundation
|
70
|
+
import Firebase
|
71
|
+
|
72
|
+
|
73
|
+
class ProductViewModel:ObservableObject{
|
74
|
+
@Published var products:[Product] = []
|
75
|
+
|
76
|
+
init() {
|
77
|
+
self.fetchProducts()
|
78
|
+
}
|
79
|
+
|
80
|
+
|
81
|
+
func fetchProducts() {
|
82
|
+
PRODUCTS_REF.observeSingleEvent(of: .value) { (snapshot) in
|
83
|
+
guard let allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }
|
84
|
+
allObjects.forEach { (snapshot) in
|
85
|
+
let productId = snapshot.key
|
86
|
+
|
87
|
+
Database.fetchProduct(with: productId) { (product) in
|
88
|
+
self.products.append(product)
|
89
|
+
}
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
```
|