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

質問編集履歴

3

誤り修正

2019/12/07 16:27

投稿

hameji
hameji

スコア1380

title CHANGED
File without changes
body CHANGED
@@ -98,7 +98,6 @@
98
98
  default: break
99
99
  }
100
100
  }
101
- }
102
101
 
103
102
  // collectionCellのtextFieldなどにdelegateを設定し、
104
103
  // returnする場合にrowを数値で指定せずに、cellPresenterのself.cellsの
@@ -113,6 +112,8 @@
113
112
  }
114
113
  self.collectionView.reloadItems(at: [[0, row]])
115
114
  }
115
+ }
116
+
116
117
  ```
117
118
 
118
119
  ![イメージ説明](8dd64db9d9c36ed73978eb2d18d34b5b.png)

2

コード修正

2019/12/07 16:27

投稿

hameji
hameji

スコア1380

title CHANGED
File without changes
body CHANGED
@@ -15,29 +15,64 @@
15
15
  簡略化したサンプルを載せてみます。
16
16
  ```Swift
17
17
  // CDCellのEnum
18
- enum CDCellPresenter {
18
+ enum CDPresentCell {
19
- case titleCell(String)
19
+ case titleCell(String?)
20
- case artistCell(String)
20
+ case artistCell(String?)
21
- case releaseCell(Date)
21
+ case releaseCell(Date?)
22
22
  }
23
23
 
24
24
  // CollectionView
25
25
  import UIKit
26
26
 
27
- class CDViewController: UIViewController {
27
+ class CDViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
28
28
  @IBOutlet weak var collectionView: UICollectionView!
29
+ @IBOutlet weak var datePicker: UIDatePicker!
29
- var cells:[CDCellPresenter]
30
+ var cells:[CDPresentCell]
30
31
 
31
32
  init() {
32
- self.cells = [.titleCell("光の気配"),
33
+ self.cells = [.titleCell(nil),
33
- .artistCell("KinKi Kids"),
34
+ .artistCell(nil),
34
- .releaseCell(Date())
35
+ .releaseCell(nil)
35
36
  ]
36
37
  }
37
38
 
39
+ // 試した1個目 <- CDPresentCell と CDPresentCellが一致しないというエラーが出ました。
40
+ private fun returnRow1(cell: CDPresentCell) -> Int? {
41
+ var m: Int? = nil
42
+ for n in 0 ..< self.cells.count {
43
+ if cell == self.cells[n] {
44
+ m = n
45
+ }
46
+ }
47
+ return m
48
+ }
49
+
50
+ // コレ試した2個目 <- CDPresentCell と CDPresentCellが一致しないというエラーが出ました。
51
+ private func returnRow2(cell: CDPresentCell) -> Int? {
52
+ if let index = self.cells.firstIndex(where: { if case cell.self = $0 { return true }; return false }) {
53
+ return index
54
+ }
55
+ return nil
56
+ }
57
+
38
58
  override func viewDidLoad() {
39
59
  }
40
60
 
61
+ @IBAction func doneButtonPressed(_ sender: Any) {
62
+ if let row = self.collectionView.indexPathsForSelectedItems?.first?.row {
63
+ let cellType = self.cells[row]
64
+ switch cellType {
65
+ case .releaseCell(let data):
66
+ self.datePicker.alpha = 1.0
67
+ default: break
68
+ }
69
+ }
70
+ }
71
+
72
+ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
73
+ return self.cells.count
74
+ }
75
+
41
76
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
42
77
  let cellType = self.cells[indexPath.row]
43
78
  switch cellType {
@@ -49,11 +84,35 @@
49
84
  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "artist", for: indexPath) as! ArtistCell
50
85
  cell.bind(data: data)
51
86
  return cell
52
- case .artistCell(let data):
87
+ case .releaseCell(let data):
53
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "artist", for: indexPath) as! ArtistCell
88
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "release", for: indexPath) as! ReleaseCell
54
89
  cell.bind(data: data)
55
90
  return cell
91
+ }
92
+
93
+ func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
94
+ let cellType = self.cells[row]
95
+ switch cellType {
96
+ case .releaseCell(let data):
97
+ self.datePicker.alpha = 1.0
98
+ default: break
99
+ }
100
+ }
56
101
  }
102
+
103
+ // collectionCellのtextFieldなどにdelegateを設定し、
104
+ // returnする場合にrowを数値で指定せずに、cellPresenterのself.cellsの
105
+ // enumからindexを取得し、更新に利用したいです。
106
+ func artistTextFieldDidReturn(text: String) {
107
+ let row = returnRow2(cell: .artist) // コレが働くようにしたい
108
+ let cellType = self.cells[row]
109
+ switch cellType {
110
+ case .titleCell(let data):
111
+ self.cells[row] = .titleCell(text)
112
+ default: break
113
+ }
114
+ self.collectionView.reloadItems(at: [[0, row]])
115
+ }
57
116
  ```
58
117
 
59
118
  ![イメージ説明](8dd64db9d9c36ed73978eb2d18d34b5b.png)

1

画像追加

2019/12/07 16:25

投稿

hameji
hameji

スコア1380

title CHANGED
File without changes
body CHANGED
@@ -53,8 +53,7 @@
53
53
  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "artist", for: indexPath) as! ArtistCell
54
54
  cell.bind(data: data)
55
55
  return cell
56
+ }
57
+ ```
56
58
 
57
-
58
-
59
-
60
- ```
59
+ ![イメージ説明](8dd64db9d9c36ed73978eb2d18d34b5b.png)