回答編集履歴

2

修正

2016/10/21 23:36

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -227,3 +227,189 @@
227
227
  }
228
228
 
229
229
  ```
230
+
231
+
232
+
233
+ 回答追記
234
+
235
+ ---
236
+
237
+
238
+
239
+ Delegateを使用してセルのボタンが変更された時に、データを変更して戻しています。
240
+
241
+
242
+
243
+ ```swift
244
+
245
+ import UIKit
246
+
247
+
248
+
249
+ // ----- ViewController -----
250
+
251
+
252
+
253
+ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomTableViewCellDelegate {
254
+
255
+
256
+
257
+ @IBOutlet weak var tableView: UITableView!
258
+
259
+ private var dataArray: Array<CellObject> = []
260
+
261
+
262
+
263
+ override func viewDidLoad() {
264
+
265
+ super.viewDidLoad()
266
+
267
+
268
+
269
+ createCellObject()
270
+
271
+ }
272
+
273
+
274
+
275
+ func createCellObject() {
276
+
277
+ dataArray.append(CellObject(title: "title1", isSelect: true))
278
+
279
+ dataArray.append(CellObject(title: "title2", isSelect: false))
280
+
281
+ dataArray.append(CellObject(title: "title3", isSelect: true))
282
+
283
+ dataArray.append(CellObject(title: "title4", isSelect: false))
284
+
285
+ }
286
+
287
+
288
+
289
+
290
+
291
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
292
+
293
+ return dataArray.count
294
+
295
+ }
296
+
297
+
298
+
299
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
300
+
301
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! CustomTableViewCell
302
+
303
+ cell.cellObject = dataArray[indexPath.row]
304
+
305
+ cell.delegate = self
306
+
307
+ return cell
308
+
309
+ }
310
+
311
+
312
+
313
+ func updateCellObject(object: CellObject) {
314
+
315
+
316
+
317
+ dump(object)
318
+
319
+ // スイッチの状態が変更されたデータが渡されてくるのでRealmに保存
320
+
321
+ }
322
+
323
+ }
324
+
325
+
326
+
327
+ // ----- Model Calss -----
328
+
329
+
330
+
331
+ class CellObject {
332
+
333
+ var title: String!
334
+
335
+ var isSelectSwtch: Bool!
336
+
337
+
338
+
339
+ init(title: String, isSelect: Bool = false) {
340
+
341
+ self.title = title
342
+
343
+ self.isSelectSwtch = isSelect
344
+
345
+ }
346
+
347
+ }
348
+
349
+
350
+
351
+
352
+
353
+
354
+
355
+ // ----- Custom Cell -----
356
+
357
+
358
+
359
+ protocol CustomTableViewCellDelegate: class {
360
+
361
+ func updateCellObject(object: CellObject)
362
+
363
+ }
364
+
365
+
366
+
367
+ class CustomTableViewCell: UITableViewCell {
368
+
369
+
370
+
371
+ @IBOutlet weak var cellSwitch: UISwitch!
372
+
373
+ @IBOutlet weak var titleLabel: UILabel!
374
+
375
+
376
+
377
+ weak var delegate: CustomTableViewCellDelegate!
378
+
379
+
380
+
381
+ var cellObject: CellObject! {
382
+
383
+ didSet {
384
+
385
+ titleLabel?.text = cellObject.title
386
+
387
+ cellSwitch.isOn = cellObject.isSelectSwtch
388
+
389
+ }
390
+
391
+ }
392
+
393
+
394
+
395
+ @IBAction func changeSwitch(_ sender: UISwitch) {
396
+
397
+
398
+
399
+ cellSwitch.isOn = !cellSwitch.isOn
400
+
401
+ cellObject.isSelectSwtch = cellSwitch.isOn
402
+
403
+
404
+
405
+ // DelegateでViewControllerに処理を渡す
406
+
407
+ delegate?.updateCellObject(object: cellObject)
408
+
409
+ }
410
+
411
+ }
412
+
413
+
414
+
415
+ ```

1

修正

2016/10/21 23:35

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -1,4 +1,4 @@
1
- あまり`ViewController`自体で`cell`のイベント受けることはやらないですが、今の実装でスイッチの切り替えCellのindexPathが必要なら`UISwitch`のサブクラスを作成してcellのindexPathを保持するプロパティを追加すれば良いと思います。
1
+ あまり`ViewController`自体で`cell`のイベント受けることはやらないですが、今の実装でスイッチの切り替えによりCellのindexPathが必要なら`UISwitch`のサブクラスを作成してcellのindexPathを保持するプロパティを追加すれば良いと思います。
2
2
 
3
3
  ※ コードは最低限で書いています(動確済)
4
4