teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

notification周りのコードを記載しました。

2015/07/16 13:02

投稿

ia_isier
ia_isier

スコア29

title CHANGED
File without changes
body CHANGED
@@ -21,4 +21,62 @@
21
21
  【追記】
22
22
  APIはCakePHPで用意しており、URLを指定してAlamofireでPOSTしています。
23
23
  Cake側ではPOSTされたコメント内容をDBに格納しています。
24
- APIを実行完了したタイミングでNotificationで通知を出し、そのタイミングでTableView.reloadData()を実行してもテーブルを再描画することができませんでした。
24
+ APIを実行完了したタイミングでNotificationで通知を出し、そのタイミングでTableView.reloadData()を実行してもテーブルを再描画することができませんでした。
25
+
26
+
27
+ 【再追記】
28
+ Notification付近のコードは以下のとおりです。
29
+ (エラーハンドリング周りは参考書からそのまま引っ張ってきています…)
30
+ 上記のUIButtonをタップした際に呼び出しているメソッドになります。
31
+ sendData()メソッドでAPIを叩いてDBに保存する形になります。
32
+ DBの値はviewWillAppear()内で最初にAPIを通して取得しています。
33
+ TableView.reloadData()メソッドの使い方を勘違いしていて、
34
+ この実装では新しくDBの値を取得できていない、ということでしょうか…?
35
+
36
+ お手数おかけして恐縮ですが、よろしくお願いします。
37
+
38
+ ```swift
39
+ func onClickSendButton(sender: UIButton) {
40
+
41
+ var qc = PostCommentQueryCondition() // POSTする際のパラメータ
42
+ var comment: String = self.editField.text
43
+
44
+ qc.comment = comment
45
+
46
+ postCommentAPI = PostCommentAPI(condition: qc)
47
+ postCommentAPI.sendData()
48
+
49
+ postCommentObserver = NSNotificationCenter.defaultCenter().addObserverForName(
50
+ postCommentAPI.PostCommentAPISendCompleteNotification,
51
+ object: nil,
52
+ queue: nil,
53
+ usingBlock: {
54
+ (notification) in
55
+
56
+ self.editField.text = ""
57
+ self.myTableView.reloadData()
58
+ self.hideKeyBoard()
59
+
60
+ // エラーがあればダイアログを開く
61
+ if notification.userInfo != nil {
62
+ if let userInfo = notification.userInfo as? [String: String!] {
63
+ if userInfo["error"] != nil {
64
+ let alertView = UIAlertController(
65
+ title: "通信エラー",
66
+ message: "通信エラーが発生しました",
67
+ preferredStyle: .Alert)
68
+ alertView.addAction(
69
+ UIAlertAction(title: "OK", style: .Default) {
70
+ action in return
71
+ }
72
+ )
73
+ self.presentViewController(alertView, animated: true, completion: nil)
74
+ }
75
+ }
76
+ }
77
+
78
+ })
79
+
80
+
81
+ }
82
+ ```

1

API周りの説明について追記しました。

2015/07/16 13:02

投稿

ia_isier
ia_isier

スコア29

title CHANGED
File without changes
body CHANGED
@@ -15,4 +15,10 @@
15
15
 
16
16
 
17
17
  どなたか対処法をご存知のかた、ご教授いただけますと助かります。
18
- よろしくお願いします。
18
+ よろしくお願いします。
19
+
20
+
21
+ 【追記】
22
+ APIはCakePHPで用意しており、URLを指定してAlamofireでPOSTしています。
23
+ Cake側ではPOSTされたコメント内容をDBに格納しています。
24
+ APIを実行完了したタイミングでNotificationで通知を出し、そのタイミングでTableView.reloadData()を実行してもテーブルを再描画することができませんでした。