質問編集履歴

2

加筆

2018/09/12 09:46

投稿

kkkkkkkt
kkkkkkkt

スコア13

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,12 @@
1
1
  Firebaseから配列を取得して、その配列によってテーブルビューのセルの数や内容を更新したいと思っているのですが、どのタイミングで変数を更新したらいいのでしょうか。以下のプログラムでは配列がhogearrayに代入はされるのですが、テーブルビューの更新ができません。
2
2
 
3
3
  よろしくお願いします。
4
+
5
+
6
+
7
+ <編集>
8
+
9
+ 以下のようにコードを編集しました。
4
10
 
5
11
 
6
12
 
@@ -8,148 +14,186 @@
8
14
 
9
15
  import Foundation
10
16
 
11
- import UIKit
17
+ import UIKit
12
18
 
13
- import Firebase
19
+ import Firebase
14
-
15
-
16
-
17
- class OrderListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
18
-
19
- // インスタンス変数
20
-
21
- var DBRef:DatabaseReference!
22
-
23
- var hogearray : [String] = []
24
-
25
- var array1 : [String] = []
26
20
 
27
21
 
28
22
 
29
-
23
+ class OrderListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
30
24
 
31
-
25
+ // インスタンス変数
32
26
 
33
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
27
+ var DBRef:DatabaseReference!
28
+
29
+ var hogearray : [String] = []
30
+
31
+ var array1 : [String] = []
34
32
 
35
33
 
36
34
 
37
- return self.hogearray.count
38
-
39
- }
40
-
41
-
42
-
43
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
44
-
45
- let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath)
46
-
47
- cell.textLabel!.text=self.hogearray[indexPath.row]
48
-
49
- return cell
50
-
51
- }
52
-
53
-
54
-
55
- override func viewDidLoad() {
56
-
57
- super.viewDidLoad()
58
-
59
- //インスタンスを作成
60
-
61
- DBRef = Database.database().reference()
35
+ @IBOutlet weak var tableView: UITableView!
62
36
 
63
37
 
64
38
 
65
- //繰り返し
39
+
66
40
 
67
- Timer.scheduledTimer( //TimerクラスのメソッドなのでTimerで宣言
41
+
68
42
 
69
- timeInterval: 0.5, //処理を行う間隔の秒
43
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
70
44
 
71
- target: self, //指定した処理を記述するクラスのインスタンス
45
+
72
46
 
73
- selector: #selector(self.newArray(_:)), //実行されるメソッド名
47
+ return self.hogearray.count
74
48
 
75
- userInfo: nil, //selectorで指定したメソッドに渡す情報
49
+ }
76
50
 
77
- repeats: true //処理を繰り返すか否か
51
+
78
52
 
79
- )
53
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
80
54
 
81
- }
55
+ let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath)
82
56
 
83
-
57
+ cell.textLabel!.text=self.hogearray[indexPath.row]
84
58
 
85
- @objc func newArray(_ sender: Timer) { //(_ sender: Timer) Timerクラスのインスタンスを受け取る
59
+ return cell
86
60
 
87
- self.hogearray = []
61
+ }
88
62
 
89
- let defaultPlace = DBRef.child("table/orderorder")
63
+
90
64
 
91
- defaultPlace.observe(.value, with: { snapshot in
65
+ override func viewDidLoad() {
92
66
 
93
- for item in (snapshot.children) {
67
+ super.viewDidLoad()
94
68
 
95
- let snapshot = item as! DataSnapshot
69
+ //インスタンスを作成
96
70
 
97
- let dict = snapshot.value as! String
71
+ DBRef = Database.database().reference()
98
72
 
99
- self.hogearray.append(dict)
73
+
100
74
 
101
- }
75
+ //繰り返し
102
76
 
103
- })
77
+ Timer.scheduledTimer( //TimerクラスのメソッドなのでTimerで宣言
104
78
 
105
- }
79
+ timeInterval: 0.5, //処理を行う間隔の秒
106
80
 
107
-
81
+ target: self, //指定した処理を記述するクラスのインスタンス
108
82
 
109
- override func didReceiveMemoryWarning() {
83
+ selector: #selector(self.newArray(_:)), //実行されるメソッド名
110
84
 
111
- super.didReceiveMemoryWarning()
85
+ userInfo: nil, //selectorで指定したメソッドに渡す情報
112
86
 
87
+ repeats: true //処理を繰り返すか否か
88
+
89
+ )
90
+
113
- }
91
+ }
92
+
93
+
94
+
95
+ @objc func newArray(_ sender: Timer) { //(_ sender: Timer) Timerクラスのインスタンスを受け取る
96
+
97
+ self.hogearray = []
98
+
99
+ let defaultPlace = DBRef.child("table/orderorder")
100
+
101
+ defaultPlace.observe(.value, with: { snapshot in
102
+
103
+ for item in (snapshot.children) {
104
+
105
+ let snapshot = item as! DataSnapshot
106
+
107
+ let dict = snapshot.value as! String
108
+
109
+ self.hogearray.append(dict)
110
+
111
+ }
112
+
113
+
114
+
115
+ })
116
+
117
+ }
118
+
119
+
120
+
121
+ override func didReceiveMemoryWarning() {
122
+
123
+ super.didReceiveMemoryWarning()
124
+
125
+ }
114
126
 
115
127
  }
128
+
129
+
116
130
 
117
131
  ```
118
132
 
119
133
 
120
134
 
121
- 追記
122
-
123
- newArrayメソッドを、以下のように書き換えました。
135
+ しかしまだセルが更新できません。そこで変数を追って見たところ、newArrayメソッドの中で以下の位置でprintするとhogearrayが取得できている(例:[1,2,3])ようなのですが、
124
136
 
125
137
  ```Swift4
126
138
 
127
139
  @objc func newArray(_ sender: Timer) { //(_ sender: Timer) Timerクラスのインスタンスを受け取る
128
140
 
129
- self.hogearray = []
141
+ self.hogearray = []
130
142
 
131
- let defaultPlace = DBRef.child("table/orderorder")
143
+ let defaultPlace = DBRef.child("table/orderorder")
132
144
 
133
- defaultPlace.observe(.value, with: { snapshot in
145
+ defaultPlace.observe(.value, with: { snapshot in
134
146
 
135
- for item in (snapshot.children) {
147
+ for item in (snapshot.children) {
136
148
 
137
- let snapshot = item as! DataSnapshot
149
+ let snapshot = item as! DataSnapshot
138
150
 
139
- let dict = snapshot.value as! String
151
+ let dict = snapshot.value as! String
140
152
 
141
- self.hogearray.append(dict)
153
+ self.hogearray.append(dict)
142
154
 
143
- }
155
+ }
144
156
 
145
-
157
+ print (self.hogearray)
146
158
 
147
- })
159
+ })
148
160
 
149
- self.tableView.reloadData()
161
+
150
162
 
151
- }
163
+ }
152
164
 
153
165
  ```
154
166
 
167
+ 以下の位置では"[]"が出力されます。
168
+
169
+ ```Swift4
170
+
155
- すると、self.tableView.reloadData()の行に"Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)'"というエーが出ています。
171
+ @objc func newArray(_ sender: Timer) { //(_ sender: Timer) Timerスのインスタンスを受け取る
172
+
173
+ self.hogearray = []
174
+
175
+ let defaultPlace = DBRef.child("table/orderorder")
176
+
177
+ defaultPlace.observe(.value, with: { snapshot in
178
+
179
+ for item in (snapshot.children) {
180
+
181
+ let snapshot = item as! DataSnapshot
182
+
183
+ let dict = snapshot.value as! String
184
+
185
+ self.hogearray.append(dict)
186
+
187
+ }
188
+
189
+
190
+
191
+ })
192
+
193
+ print (self.hogearray)
194
+
195
+ }
196
+
197
+ ```
198
+
199
+ メソッド外で変数が共有できていないということになると思うのですが、どこに原因があるのでしょうか。

1

加筆

2018/09/12 09:46

投稿

kkkkkkkt
kkkkkkkt

スコア13

test CHANGED
File without changes
test CHANGED
@@ -115,3 +115,41 @@
115
115
  }
116
116
 
117
117
  ```
118
+
119
+
120
+
121
+ 追記
122
+
123
+ newArrayメソッドを、以下のように書き換えました。
124
+
125
+ ```Swift4
126
+
127
+ @objc func newArray(_ sender: Timer) { //(_ sender: Timer) Timerクラスのインスタンスを受け取る
128
+
129
+ self.hogearray = []
130
+
131
+ let defaultPlace = DBRef.child("table/orderorder")
132
+
133
+ defaultPlace.observe(.value, with: { snapshot in
134
+
135
+ for item in (snapshot.children) {
136
+
137
+ let snapshot = item as! DataSnapshot
138
+
139
+ let dict = snapshot.value as! String
140
+
141
+ self.hogearray.append(dict)
142
+
143
+ }
144
+
145
+
146
+
147
+ })
148
+
149
+ self.tableView.reloadData()
150
+
151
+ }
152
+
153
+ ```
154
+
155
+ すると、self.tableView.reloadData()の行に"Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)'"というエラーが出ています。