質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Delegates

Delegatesとは、オブジェクト指向型プログラミングにおいて、あるオブジェクトの操作を一部の他のオブジェクトに代替させる手法のこと。オブジェクトは他のデリゲートに頼って関数を実行することができます。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

2回答

268閲覧

swift4 CustomTableViewCellのdelegateが表示されない

hameji001

総合スコア639

Delegates

Delegatesとは、オブジェクト指向型プログラミングにおいて、あるオブジェクトの操作を一部の他のオブジェクトに代替させる手法のこと。オブジェクトは他のデリゲートに頼って関数を実行することができます。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2018/12/14 05:25

質問させてください。
customcellにtextfieldを設置し、tableviewでcellとして使用したく、
http://an.hatenablog.jp/entry/2015/10/04/015712
のブログを参考に作成しております。

メインのviewからcellをselectし、
keyboardを表示することはできるのですが、
入力後、完了などのボタンを押しても、全く反応がなく、
delegateのfunctionがうまく接続できていないようです。
もちろんcell.delegate = selfも設定済みです。

swiftのバージョンアップに伴うコード修正は自動で入ってその通り直しておりますが、
どこがうまく繋がらない原因でしょうか???
ご教授いただけると幸いです。

ほとんど参考のブログのままですが、
以下はメインのページでのコードの抜粋になります。

Swift

1// MARK: - InputTextTableCellDelegate 2 func textFieldDidEndEditing(cell: InputTextTableCell, value: NSString) -> () { 3 let path = tableView.indexPathForRow(at: cell.convert(cell.bounds.origin, to: tableView)) 4 NSLog("row = %d, value = %@", path!.row, value) 5 } 6}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

rocket

2018/12/14 06:26

もっと詳細を出してもらった方が良いかと。 コードの抜粋がその部分だけなので参考にされたブログと実際にされた実装の違いも分からない状態です。 例えばプロトコルができてないのではという風に推測を立てられるぐらいなので答えづらいかと、、、。 また、もっと単純に完了のボタンを押した時にメソッドを呼び出す方法を調べて見るとよいかとおもいました。 > 完了などのボタンを押しても、全く反応がなく というのはどういう事なのか、画面に反応がないのか、そもそもdlegateメソッド自体は呼び出されているのかいないのか気になりました。
hameji001

2018/12/14 09:37 編集

ご指摘ごもっともで申し訳ございません。 全体コードは冗長となってしまうので、抜粋して、追記させていただきます。 # TextFieldCell.swift import UIKit @objc protocol TextFieldCellDelegate { func textFieldDidEndEditing(cell: TextFieldCell, value: NSString) -> () func textFieldShouldReturn(cell: TextFieldCell, value: NSString) -> () } class TextFieldCell: UITableViewCell, UITextFieldDelegate { weak var delegate: TextFieldCellDelegate! = nil @IBOutlet weak var itemLabel: UILabel! @IBOutlet weak var valueTextField: UITextField! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } // MARK: - UITextFieldDelegate func textFieldShouldReturn(_ textField: UITextField) -> Bool { self.delegate.textFieldShouldReturn(cell: self, value: textField.text! as NSString) textField.resignFirstResponder() return true } func textFieldDidEndEditing(_ textField: UITextField) { self.delegate.textFieldDidEndEditing(cell: self, value: textField.text! as NSString) } } # TableViewController.swift import UIKit class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, TextFieldCellDelegate { @IBOutlet weak var tableView: UITableView! let itemList = ["名称", "値段"] var valueList = ["", ""] var toolbar = UIToolbar() override func viewDidLoad() { super.viewDidLoad() toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 0, height: 35)) let cancelItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(TableViewController.cancel)) let spacerItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(TableViewController.done)) toolbar.setItems([cancelItem, spacerItem, doneItem], animated: true) } @objc func done() { let cindexPath = tableView.indexPathForSelectedRow! let cell = self.tableView.cellForRow(at: cindexPath) as! TextFieldCell cell.valueTextField.resignFirstResponder() tableView.isScrollEnabled = true tableView.allowsSelection = true tableView.deselectRow(at: tableView.indexPathForSelectedRow!, animated: true) } @objc func cancel() { let cell = self.tableView.cellForRow(at: tableView.indexPathForSelectedRow!) as! TextFieldCell cell.valueTextField.text?.removeAll() cell.valueTextField.resignFirstResponder() tableView.isScrollEnabled = true tableView.allowsSelection = true tableView.deselectRow(at: tableView.indexPathForSelectedRow!, animated: true) } // MARK: /_/_/_/_/ TableView /_/_/_/_/ func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return itemList.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell") as! TextFieldCell cell.delegate = self cell.valueTextField.isEnabled = false cell.itemLabel.text = itemList[indexPath.row] cell.valueTextField?.returnKeyType = .done cell.valueTextField?.text = valueList[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("selected", indexPath) tableView.allowsSelection = false tableView.isScrollEnabled = false let cell = tableView.cellForRow(at: indexPath) as! TextFieldCell cell.valueTextField.isEnabled = true switch indexPath.row { case 0: // 名称 cell.valueTextField.keyboardType = .default case 1: // 値段 cell.valueTextField.keyboardType = .numberPad cell.valueTextField.inputAccessoryView = toolbar default: break } cell.valueTextField.becomeFirstResponder() } // MARK: - textFieldCellDelegate func textFieldDidEndEditing(cell: TextFieldCell, value: NSString) -> () { let path = tableView.indexPathForRow(at: cell.convert(cell.bounds.origin, to: tableView)) print("row = %d, value = %@", path!.row, value) } func textFieldShouldReturn(cell: TextFieldCell, value: NSString) -> () { print("return pressed") let cell = tableView.cellForRow(at: tableView.indexPathForSelectedRow!) as! TextFieldCell cell.valueTextField.resignFirstResponder() } } 長くなってしまいました。ちなみにもちろんビルドはできて、 keyboardのbecomFirstResponder、resignFirstResponderに delegate以外からはアクセスできます。 しかし、keyboardのreturn(完了)を押しても、 printはコンソールに表示されません。 よろしくお願いします。
takabosoft

2018/12/17 00:38

解決してよかったです。追加情報のソースはこのコメント欄ではなく、質問内容を「編集」して載せたほうが回答者は回答しやすいですので次からそうしてみてください。
guest

回答2

0

自己解決

いろいろdelegateについて調べていたら、

https://medium.com/@aapierce0/swift-using-protocols-to-add-custom-behavior-to-a-uitableviewcell-2c1f09610aa1

にたどり着き、最終的にコードを下記のように変えることで解決できました。
どうもcustomcellのvalueTextFieldのdelegateを指定していなかったので、
コードと接続されていなかったみたいです。

swift

1# TextFieldCell.swift 2 3import UIKit 4 5@objc protocol TextFieldCellDelegate { 6 func textFieldDidEndEditing(cell: TextFieldCell, value: NSString) -> () 7 func textFieldShouldReturn(cell: TextFieldCell, value: NSString) -> () 8} 9 10class TextFieldCell: UITableViewCell, UITextFieldDelegate { 11 weak var delegate: TextFieldCellDelegate! = nil 12 13 @IBOutlet weak var itemLabel: UILabel! 14 @IBOutlet weak var valueTextField: UITextField! 15 16 override func awakeFromNib() { 17 super.awakeFromNib() 18 self.valueTextField.delegate = self // <-これを追加しました。 19 } 20 21 override func setSelected(_ selected: Bool, animated: Bool) { 22 super.setSelected(selected, animated: animated) 23 } 24 25 // MARK: - UITextFieldDelegate 26 func textFieldShouldReturn(_ textField: UITextField) -> Bool { 27 self.delegate.textFieldShouldReturn(cell: self, value: textField.text! as NSString) 28 textField.resignFirstResponder() 29 return true 30 } 31 32 func textFieldDidEndEditing(_ textField: UITextField) { 33 self.delegate.textFieldDidEndEditing(cell: self, value: textField.text! as NSString) 34 } 35}

投稿2018/12/14 13:09

hameji001

総合スコア639

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

InputTextTableCellの

internal func textFieldDidEndEditing(textField: UITextField) {

が微妙に違うので

func textFieldDidEndEditing(_ textField: UITextField) {

に変更して試してみてください(関係ないかもしれませんが(^_^;))。

中のself.delegate.textFieldDidEndEditing(self, value: textField.text!)の行にブレークポイントを貼ってそこでブレークする事が最低条件かなと思われます。

投稿2018/12/14 07:36

takabosoft

総合スコア8356

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hameji001

2018/12/14 08:53

回答ありがとうございます。 ご指摘のところはXcodeの機能でアラートが出るので、 textFieldDidEndEditing(_ textField: UITextField)と元からなっておりました。 internalは外してみましたが、何れにしてもNSLogやprintに反応しない状況が続いております。。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問