質問編集履歴

1

コードを追記しました。

2021/09/06 09:56

投稿

smawe
smawe

スコア0

test CHANGED
File without changes
test CHANGED
@@ -6,6 +6,136 @@
6
6
 
7
7
  コード
8
8
 
9
+ import UIKit
10
+
11
+ import Firebase
12
+
13
+
14
+
15
+ class roomlist:UIViewController{
16
+
17
+ private let cellId = "cellId"
18
+
19
+ private var rooms = [Room]()
20
+
21
+ private var selectedRoom : Room?
22
+
23
+
24
+
25
+ @IBOutlet weak var roomlisttable: UITableView!
26
+
27
+
28
+
29
+ override func viewDidLoad() {
30
+
31
+ super.viewDidLoad()
32
+
33
+
34
+
35
+ roomlisttable.delegate = self
36
+
37
+ roomlisttable.dataSource = self
38
+
39
+ fechroominfofirestore()
40
+
41
+
42
+
43
+ }
44
+
45
+ private func fechroominfofirestore(){
46
+
47
+
48
+
49
+ Firestore.firestore().collection("rooms").getDocuments{(snapshots,error) in
50
+
51
+ // print("uuuuuuuuu",Firestore.firestore().collection("rooms").document(""))
52
+
53
+
54
+
55
+ if error != nil{
56
+
57
+ print("sippai")
58
+
59
+
60
+
61
+ return
62
+
63
+
64
+
65
+ }
66
+
67
+ snapshots?.documents.forEach({ (snapshots) in
68
+
69
+ let dic = snapshots.data()
70
+
71
+ let room = Room.init(dic: dic)
72
+
73
+
74
+
75
+ self.rooms.append(room)
76
+
77
+ self.roomlisttable.reloadData()
78
+
79
+
80
+
81
+ self.rooms.forEach{(rooms) in
82
+
83
+ print("room",room.bsubname)
84
+
85
+
86
+
87
+ }
88
+
89
+ })
90
+
91
+
92
+
93
+ }
94
+
95
+ }
96
+
97
+ }
98
+
99
+ extension roomlist:UITableViewDelegate, UITableViewDataSource{
100
+
101
+
102
+
103
+
104
+
105
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
106
+
107
+ return rooms.count
108
+
109
+ }
110
+
111
+
112
+
113
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
114
+
115
+ let cell = roomlisttable.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! roomlistcell
116
+
117
+ cell.room = rooms[indexPath.row]
118
+
119
+
120
+
121
+ return cell
122
+
123
+ }
124
+
125
+
126
+
127
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
128
+
129
+
130
+
131
+ print(indexPath)
132
+
133
+ print("didSelectRowAt")
134
+
135
+ tableView.deselectRow(at: indexPath, animated: false)
136
+
137
+
138
+
9
139
  Firestore.firestore().collection("rooms").getDocuments() { (querySnapshot, err) in
10
140
 
11
141
  if let err = err {
@@ -18,14 +148,112 @@
18
148
 
19
149
  let documentID = doc.documentID
20
150
 
151
+
152
+
153
+ print(documentID)
154
+
21
155
  }
22
156
 
23
157
  }
24
158
 
25
159
  }
26
160
 
161
+
162
+
163
+
164
+
165
+
166
+
27
-
167
+ }
168
+
169
+
170
+
171
+ func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
172
+
173
+
174
+
175
+ }
176
+
177
+
178
+
179
+ override func prepare(for segue : UIStoryboardSegue, sender: Any?){
180
+
181
+
182
+
183
+ print("Will move to next view controller.")
184
+
185
+ switch segue.identifier {
186
+
187
+ case "RoomdetqailsViewController":
188
+
189
+ if let RoomdetqailsViewController = segue.destination as? RoomdetqailsViewController {
190
+
191
+ print("Decided a destination as RoomdetqailsViewController.")
192
+
193
+ if let indexPath = self.roomlisttable.indexPathForSelectedRow {
194
+
195
+ let room = self.rooms[indexPath.row]
196
+
197
+ RoomdetqailsViewController.received = room
198
+
199
+ }
200
+
201
+ }
202
+
203
+ default:
204
+
205
+ print("There is no identifier of segue.")
206
+
207
+ break
208
+
209
+ }
210
+
211
+ }
212
+
213
+ }
214
+
215
+ class roomlistcell : UITableViewCell{
216
+
217
+
218
+
219
+ var room: Room?{
220
+
221
+ didSet{
222
+
223
+ roomname.text = room?.bsubname
224
+
225
+ }
226
+
227
+ }
228
+
229
+
230
+
231
+ @IBOutlet weak var roomimage: UIImageView!
232
+
233
+ @IBOutlet weak var roomname: UILabel!
234
+
235
+ override func awakeFromNib() {
236
+
237
+ super.awakeFromNib()
238
+
239
+
240
+
241
+ roomimage.layer.cornerRadius = roomimage.frame.width/2
242
+
243
+ }
244
+
245
+ override func setSelected(_ selected: Bool, animated: Bool) {
246
+
247
+ super.setSelected(selected, animated: animated)
248
+
249
+ }
250
+
251
+ }
28
252
 
29
253
  ```
30
254
 
255
+
256
+
257
+
258
+
31
259
  このコードで全てのcellのドキュメントを取得する方法は実装できたのですが、タップされたcellだけのドキュメントidを取得する方法がわかたずに困っています。