質問編集履歴
2
タグの追加
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
1
ソースの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -81,3 +81,123 @@
|
|
81
81
|
果物が一列に横並びになっている部分が1ページ目表示されていなかった部分です。
|
82
82
|
|
83
83
|
![イメージ説明](830354bc42e9786ee5204a528acf5b9f.png)
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
表示画面のビューとデーター取得のソースは以下になります。
|
88
|
+
|
89
|
+
```ここに言語を入力
|
90
|
+
|
91
|
+
struct ProductListView: View {
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
@ObservedObject var productVM = ProductViewModel()
|
96
|
+
|
97
|
+
var body: some View {
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
let chunkedProducts = self.productVM.products.chunked(into: 3)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
ScrollView {
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
VStack {
|
110
|
+
|
111
|
+
ForEach(0..<chunkedProducts.count) { index in
|
112
|
+
|
113
|
+
HStack {
|
114
|
+
|
115
|
+
ForEach(chunkedProducts[index]) { product in
|
116
|
+
|
117
|
+
ProductCell(product: product)
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
ーーーー
|
136
|
+
|
137
|
+
import Foundation
|
138
|
+
|
139
|
+
import Firebase
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
class ProductViewModel:ObservableObject{
|
146
|
+
|
147
|
+
@Published var products:[Product] = []
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
init() {
|
152
|
+
|
153
|
+
self.fetchProducts()
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
func fetchProducts() {
|
162
|
+
|
163
|
+
PRODUCTS_REF.observeSingleEvent(of: .value) { (snapshot) in
|
164
|
+
|
165
|
+
guard let allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }
|
166
|
+
|
167
|
+
allObjects.forEach { (snapshot) in
|
168
|
+
|
169
|
+
let productId = snapshot.key
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
Database.fetchProduct(with: productId) { (product) in
|
174
|
+
|
175
|
+
self.products.append(product)
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
```
|