質問編集履歴

1

コードを編集し、エラーメッセージの内容を追加しました

2019/05/08 04:15

投稿

Moai101
Moai101

スコア20

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  今firebaseを使って簡単なチャットアプリを作っていて、スワイプ削除機能を実装したいと思っているのですがどのようにしたらいいかわかりません。 以下のコードの場合どのようなアプローチが取れるでしょうか?
2
2
 
3
-  下記のリンクの84行目と167行目あたりを参考にしてみたのですがそのまま取り込んでもうまくいきませんでした
3
+  下記のリンクの84行目と167行目あたりを参考にしてみたのですがそのまま取り込んでもUse of unresolved identifier 'messageDB'; did you mean 'Message'?といエラーメッセージが出てきま
4
4
 
5
5
  https://github.com/ShinokiRyosei/Firebase-Sampler/blob/3d86a0e959a062413bc13c04524da2b3996c3cd1/SampleFirebase/Controllers/ListViewController.swift
6
6
 
@@ -14,6 +14,22 @@
14
14
 
15
15
 
16
16
 
17
+ //
18
+
19
+ // ViewController.swift
20
+
21
+ // Flash Chat
22
+
23
+ //
24
+
25
+ // Created by Angela Yu on 29/08/2015.
26
+
27
+ // Copyright (c) 2015 London App Brewery. All rights reserved.
28
+
29
+ //
30
+
31
+
32
+
17
33
  import UIKit
18
34
 
19
35
  import Firebase
@@ -54,6 +70,8 @@
54
70
 
55
71
  super.viewDidLoad()
56
72
 
73
+ // identifierという言葉は画面遷移の時だけ
74
+
57
75
  messageTableView.delegate = self
58
76
 
59
77
  messageTableView.dataSource = self
@@ -62,6 +80,8 @@
62
80
 
63
81
  messageTextfield.delegate = self
64
82
 
83
+ // UIGestureRecognizer ではタップ、スワイプ(フリック)等々を扱えます
84
+
65
85
  let tapGesture = UITapGestureRecognizer(target: self, action: #selector (tableViewTapped))
66
86
 
67
87
 
@@ -78,7 +98,7 @@
78
98
 
79
99
 
80
100
 
81
- retrieveMessages()
101
+ observeMessages()
82
102
 
83
103
 
84
104
 
@@ -146,6 +166,26 @@
146
166
 
147
167
 
148
168
 
169
+ func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
170
+
171
+ //デリートボタンを追加
172
+
173
+ if editingStyle == .delete {
174
+
175
+ //選択されたCellのIndexPathを渡し、データをFirebase上から削除するためのメソッド
176
+
177
+ self.delete(at: indexPath)
178
+
179
+ //TableView上から削除
180
+
181
+ messageTableView.deleteRows(at: [indexPath], with: .fade)
182
+
183
+ }
184
+
185
+ }
186
+
187
+
188
+
149
189
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
150
190
 
151
191
  return messageArray.count
@@ -238,6 +278,8 @@
238
278
 
239
279
 
240
280
 
281
+ // sendボタンを押したら実行されるので、これを参考に削除機能を書いていけば
282
+
241
283
  @IBAction func sendPressed(_ sender: AnyObject) {
242
284
 
243
285
 
@@ -300,15 +342,21 @@
300
342
 
301
343
 
302
344
 
303
-
345
+ func delete(at indexPath: IndexPath) {
346
+
304
-
347
+ messageDB.child((Auth.auth().currentUser?.uid)!).child(messageArray[indexPath.row].key).removeValue()
348
+
305
-
349
+ messageArray.remove(at: indexPath.row)
350
+
306
-
351
+ }
307
-
308
-
309
-
310
-
352
+
353
+
354
+
355
+
356
+
357
+
358
+
311
- func retrieveMessages() {
359
+ func observeMessages() {
312
360
 
313
361
 
314
362
 
@@ -316,7 +364,9 @@
316
364
 
317
365
 
318
366
 
319
- messageDB.observe(.childAdded) { (snapshot) in
367
+ messageDB.observe(.childAdded)
368
+
369
+ { (snapshot) in
320
370
 
321
371
 
322
372
 
@@ -346,15 +396,11 @@
346
396
 
347
397
  self.messageTableView.reloadData()
348
398
 
349
-
350
-
351
-
352
-
353
-
354
-
355
- }
399
+ }
400
+
356
-
401
+ //// ここに追加かもしれない
402
+
357
-
403
+ // https://qiita.com/nakadoribooks/items/9f566065e77ea585c4b2#%E6%88%90%E6%9E%9C%E7%89%A9
358
404
 
359
405
  }
360
406
 
@@ -410,4 +456,6 @@
410
456
 
411
457
 
412
458
 
459
+
460
+
413
461
  ```