回答編集履歴
2
修正
answer
CHANGED
@@ -112,4 +112,97 @@
|
|
112
112
|
cellObject.isSelectSwtch = cellSwitch.isOn
|
113
113
|
}
|
114
114
|
}
|
115
|
+
```
|
116
|
+
|
117
|
+
回答追記
|
118
|
+
---
|
119
|
+
|
120
|
+
Delegateを使用してセルのボタンが変更された時に、データを変更して戻しています。
|
121
|
+
|
122
|
+
```swift
|
123
|
+
import UIKit
|
124
|
+
|
125
|
+
// ----- ViewController -----
|
126
|
+
|
127
|
+
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomTableViewCellDelegate {
|
128
|
+
|
129
|
+
@IBOutlet weak var tableView: UITableView!
|
130
|
+
private var dataArray: Array<CellObject> = []
|
131
|
+
|
132
|
+
override func viewDidLoad() {
|
133
|
+
super.viewDidLoad()
|
134
|
+
|
135
|
+
createCellObject()
|
136
|
+
}
|
137
|
+
|
138
|
+
func createCellObject() {
|
139
|
+
dataArray.append(CellObject(title: "title1", isSelect: true))
|
140
|
+
dataArray.append(CellObject(title: "title2", isSelect: false))
|
141
|
+
dataArray.append(CellObject(title: "title3", isSelect: true))
|
142
|
+
dataArray.append(CellObject(title: "title4", isSelect: false))
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
147
|
+
return dataArray.count
|
148
|
+
}
|
149
|
+
|
150
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
151
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! CustomTableViewCell
|
152
|
+
cell.cellObject = dataArray[indexPath.row]
|
153
|
+
cell.delegate = self
|
154
|
+
return cell
|
155
|
+
}
|
156
|
+
|
157
|
+
func updateCellObject(object: CellObject) {
|
158
|
+
|
159
|
+
dump(object)
|
160
|
+
// スイッチの状態が変更されたデータが渡されてくるのでRealmに保存
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
// ----- Model Calss -----
|
165
|
+
|
166
|
+
class CellObject {
|
167
|
+
var title: String!
|
168
|
+
var isSelectSwtch: Bool!
|
169
|
+
|
170
|
+
init(title: String, isSelect: Bool = false) {
|
171
|
+
self.title = title
|
172
|
+
self.isSelectSwtch = isSelect
|
173
|
+
}
|
174
|
+
}
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
// ----- Custom Cell -----
|
179
|
+
|
180
|
+
protocol CustomTableViewCellDelegate: class {
|
181
|
+
func updateCellObject(object: CellObject)
|
182
|
+
}
|
183
|
+
|
184
|
+
class CustomTableViewCell: UITableViewCell {
|
185
|
+
|
186
|
+
@IBOutlet weak var cellSwitch: UISwitch!
|
187
|
+
@IBOutlet weak var titleLabel: UILabel!
|
188
|
+
|
189
|
+
weak var delegate: CustomTableViewCellDelegate!
|
190
|
+
|
191
|
+
var cellObject: CellObject! {
|
192
|
+
didSet {
|
193
|
+
titleLabel?.text = cellObject.title
|
194
|
+
cellSwitch.isOn = cellObject.isSelectSwtch
|
195
|
+
}
|
196
|
+
}
|
197
|
+
|
198
|
+
@IBAction func changeSwitch(_ sender: UISwitch) {
|
199
|
+
|
200
|
+
cellSwitch.isOn = !cellSwitch.isOn
|
201
|
+
cellObject.isSelectSwtch = cellSwitch.isOn
|
202
|
+
|
203
|
+
// DelegateでViewControllerに処理を渡す
|
204
|
+
delegate?.updateCellObject(object: cellObject)
|
205
|
+
}
|
206
|
+
}
|
207
|
+
|
115
208
|
```
|
1
修正
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
あまり`ViewController`自体で`cell`のイベント受けることはやらないですが、今の実装でスイッチの切り替え
|
1
|
+
あまり`ViewController`自体で`cell`のイベント受けることはやらないですが、今の実装でスイッチの切り替えによりCellのindexPathが必要なら`UISwitch`のサブクラスを作成してcellのindexPathを保持するプロパティを追加すれば良いと思います。
|
2
2
|
※ コードは最低限で書いています(動確済)
|
3
3
|
|
4
4
|
```swift
|