回答編集履歴

5

修正

2016/08/07 04:05

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -138,12 +138,10 @@
138
138
 
139
139
 
140
140
 
141
- Containerの子配列からオブジェクトを取得する場合
141
+ Containerの子配列からオブジェクトを取得する場合
142
142
 
143
143
  ---
144
144
 
145
-
146
-
147
145
  ```swift
148
146
 
149
147
  import UIKit

4

修正

2016/08/07 04:05

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -138,6 +138,124 @@
138
138
 
139
139
 
140
140
 
141
+ Containerの子配列からオブジェクトを取得する場合
142
+
143
+ ---
144
+
145
+
146
+
147
+ ```swift
148
+
149
+ import UIKit
150
+
151
+
152
+
153
+ class ViewController: UIViewController {
154
+
155
+
156
+
157
+ var tableViewController: TableViewController!
158
+
159
+
160
+
161
+ override func viewDidLoad() {
162
+
163
+ super.viewDidLoad()
164
+
165
+
166
+
167
+ // Containerの子配列からTableViewControllerオブジェクトを取得
168
+
169
+ tableViewController = self.childViewControllers[0] as! TableViewController
170
+
171
+ }
172
+
173
+
174
+
175
+ @IBAction func containerTableReload(sender: UIButton) {
176
+
177
+
178
+
179
+ tableViewController.dataArray += ["Sample Data"]
180
+
181
+ tableViewController.reloadTable()
182
+
183
+ }
184
+
185
+ }
186
+
187
+
188
+
189
+ // --------------------------------------
190
+
191
+
192
+
193
+ class TableViewController: UITableViewController {
194
+
195
+
196
+
197
+ override func viewDidLoad() {
198
+
199
+ super.viewDidLoad()
200
+
201
+
202
+
203
+ tableView.estimatedRowHeight = 20
204
+
205
+ tableView.rowHeight = UITableViewAutomaticDimension
206
+
207
+ }
208
+
209
+
210
+
211
+ func reloadTable() {
212
+
213
+ tableView.reloadData()
214
+
215
+ }
216
+
217
+
218
+
219
+ // Data Array
220
+
221
+ var dataArray = ["Sample Data"]
222
+
223
+
224
+
225
+ // MARK: - TableView Delegate & DataSource
226
+
227
+
228
+
229
+ // Row Count
230
+
231
+ override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
232
+
233
+ return dataArray.count
234
+
235
+ }
236
+
237
+
238
+
239
+ // Generate Cell
240
+
241
+ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
242
+
243
+ let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
244
+
245
+ cell.textLabel?.numberOfLines = 0
246
+
247
+ cell.textLabel?.text = dataArray[indexPath.row]
248
+
249
+ return cell
250
+
251
+ }
252
+
253
+ }
254
+
255
+ ```
256
+
257
+
258
+
141
259
  通知を使用する場合
142
260
 
143
261
  ---

3

修正

2016/08/07 04:04

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -135,3 +135,133 @@
135
135
 
136
136
 
137
137
  ![s](3fb1a6d3a5c05f97925761dacf4af9fa.png)
138
+
139
+
140
+
141
+ 通知を使用する場合
142
+
143
+ ---
144
+
145
+
146
+
147
+ ```swift
148
+
149
+ import UIKit
150
+
151
+
152
+
153
+ class ViewController: UIViewController {
154
+
155
+
156
+
157
+ override func viewDidLoad() {
158
+
159
+ super.viewDidLoad()
160
+
161
+
162
+
163
+ }
164
+
165
+
166
+
167
+ @IBAction func containerTableReload(sender: UIButton) {
168
+
169
+ // 通知を送信
170
+
171
+ NSNotificationCenter.defaultCenter().postNotificationName("TableReloadNotification", object: ["Sample Data"])
172
+
173
+ }
174
+
175
+ }
176
+
177
+
178
+
179
+ // --------------------------------------
180
+
181
+
182
+
183
+ class TableViewController: UITableViewController {
184
+
185
+
186
+
187
+ override func viewDidLoad() {
188
+
189
+ super.viewDidLoad()
190
+
191
+
192
+
193
+ // 通知を登録
194
+
195
+ NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.reloadTable(_:)), name: "TableReloadNotification", object: nil)
196
+
197
+
198
+
199
+ tableView.estimatedRowHeight = 20
200
+
201
+ tableView.rowHeight = UITableViewAutomaticDimension
202
+
203
+ }
204
+
205
+
206
+
207
+ deinit {
208
+
209
+ // 通知を解除
210
+
211
+ NSNotificationCenter.defaultCenter().removeObserver(self)
212
+
213
+ }
214
+
215
+
216
+
217
+ func reloadTable(notification: NSNotification) {
218
+
219
+ let data = notification.object as! [String]
220
+
221
+ dataArray += data
222
+
223
+ tableView.reloadData()
224
+
225
+ }
226
+
227
+
228
+
229
+ // Data Array
230
+
231
+ var dataArray = ["Sample Data"]
232
+
233
+
234
+
235
+ // MARK: - TableView Delegate & DataSource
236
+
237
+
238
+
239
+ // Row Count
240
+
241
+ override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
242
+
243
+ return dataArray.count
244
+
245
+ }
246
+
247
+
248
+
249
+ // Generate Cell
250
+
251
+ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
252
+
253
+ let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
254
+
255
+ cell.textLabel?.numberOfLines = 0
256
+
257
+ cell.textLabel?.text = dataArray[indexPath.row]
258
+
259
+ return cell
260
+
261
+ }
262
+
263
+ }
264
+
265
+
266
+
267
+ ```

2

修正

2016/08/06 22:55

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  `ViewController`を作成するときに`prepareForSegue`が呼ばれるので、そこで`TableViewController`のインスタンスを自身に保持するやり方です。
10
10
 
11
- ※ 親から直接`reloadData()`を呼んでますが、`TableViewController`に更新するメソッドを作成してそれを呼ぶほうが良いと思います。
11
+
12
12
 
13
13
 
14
14
 
@@ -40,7 +40,7 @@
40
40
 
41
41
  tableViewController.dataArray += ["Sample Data"]
42
42
 
43
- tableViewController.tableView.reloadData()
43
+ tableViewController.reloadTable()
44
44
 
45
45
  }
46
46
 
@@ -81,6 +81,14 @@
81
81
  tableView.estimatedRowHeight = 20
82
82
 
83
83
  tableView.rowHeight = UITableViewAutomaticDimension
84
+
85
+ }
86
+
87
+
88
+
89
+ func reloadTable() {
90
+
91
+ tableView.reloadData()
84
92
 
85
93
  }
86
94
 

1

修正

2016/08/06 22:30

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -7,6 +7,8 @@
7
7
 
8
8
 
9
9
  `ViewController`を作成するときに`prepareForSegue`が呼ばれるので、そこで`TableViewController`のインスタンスを自身に保持するやり方です。
10
+
11
+ ※ 親から直接`reloadData()`を呼んでますが、`TableViewController`に更新するメソッドを作成してそれを呼ぶほうが良いと思います。
10
12
 
11
13
 
12
14