現在以下の画面のようにtableviewのセルに3つtextfieldがあります。
そして画面右上のOKが押されるとtextfieldの値がrealmに保存されるようにしていたのですが、コードを書いている時にtextfieldの編集が終わった時点でrealmに値が保存されることに気づきました。コードは以下のようになっています。
swift
1func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { 2 let realm = try! Realm() 3 4 switch textField.tag { 5 case CellTextField.name.rawValue: 6 try! realm.write { 7 self.product?.name = textField.text 8 } 9 10 case CellTextField.category.rawValue: 11 try! realm.write { 12 self.product?.category = textField.text 13 } 14 15 case CellTextField.price.rawValue: 16 try! realm.write { 17 if !textField.text!.isEmpty { 18 self.product?.price.value = Int(textField.text!) 19 } 20 } 21 22 default: 23 break 24 } 25 return true 26 }
textfieldを編集しても画面左上のCencelが押されれば値は保存されないようにしたいので上記のコードではダメだと思いました。
なのでOKボタンのアクションのハンドラ内でrealmへの保存を行いたいのですが、realmでは値の保存を遅らせたりすることはできるのでしょうか?
その他例えばダミーのProductクラスを一つ作って、上記コードのtextFieldShouldEndEditingでそのダミーProductのインスタンスに値を入れつつ、OKが押された時にダミーのProductをrealmのProductに流しこもうかなと思ったのですが、手間がかかって面倒だと思いました......。
realmへの値の保存を遅らせるだけでなく、その他何かいい方法があれば教えていただきたいです。
よろしくお願いします。
///回答を受けての追記///
swift
1 let realm = try! Realm() 2 self.view.subviews.forEach({ view in 3 if let textField = view as? NameEditTableViewCell { 4 try! realm.write { 5 self.product?.name = textField.textLabel?.text 6 } 7 } 8 if let textField = view as? CategoryEditTableViewCell { 9 try! realm.write { 10 self.product?.name = textField.textLabel?.text 11 } 12 } 13 if let textField = view as? PriceEditTableViewCell { 14 try! realm.write { 15 self.product?.name = textField.textLabel?.text 16 } 17 } 18 })
///更なる追記///
1:
swift
1class NameEditTableViewCell: UITableViewCell { 2 3 @IBOutlet weak var nameTextField: UITextField! 4 5} 6 7class CategoryEditTableViewCell: UITableViewCell { 8 9 @IBOutlet weak var categoryTextField: UITextField! 10} 11 12class PriceEditTableViewCell: UITableViewCell { 13 14 @IBOutlet weak var priceTextField: UITextField! 15 16}
2: はい、viewcontrollerのフィールドにstoryboardのtableviewから引っ張ってきたtableviewのインスタンスがあります。
3:
swift
1func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 2 let type = self.displayCells()[indexPath.row] 3 let id = type.rawValue 4 let cell = self.tableView.dequeueReusableCell(withIdentifier: id, for: indexPath) 5 self.configureCell(targetCell: cell, type: type) 6 7 return cell 8 } 9 10func configureCell(targetCell: UITableViewCell, type: CellType) { 11 switch type { 12 case .name: 13 if let cell = targetCell as? NameEditTableViewCell { 14 cell.nameTextField.text = self.product?.name 15 cell.nameTextField.delegate = self 16 } 17 case .category: 18 if let cell = targetCell as? CategoryEditTableViewCell { 19 cell.categoryTextField.text = self.product?.category 20 cell.categoryTextField.delegate = self 21 } 22 case .price: 23 if let cell = targetCell as? PriceEditTableViewCell { 24 if let price = self.product?.price.value { 25 cell.priceTextField.text = String(describing: price) 26 } 27 cell.priceTextField.delegate = self 28 } 29 } 30 } 31
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/01/10 12:52
2017/01/10 13:25
2017/01/10 22:56
2017/01/11 01:38
2017/01/11 12:08
2017/01/11 12:10