###前提・実現したいこと
<Swift>
TODOアプリのように、TableViewCell内にTextFieldを組み込んで、
直接文字入力ができるような機能を実装したいと思い、
ネット上で参考になりそうなサンプルを探して実装してみたのですが、エラーが出てしまいます。
どのように改善すればいいかわからずに悩んでいます。
もし、お詳しい方がいらっしゃれば、ご享受いただければ幸いです。
###ソースコード
ViewController.swift
import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, InputTextTableCellDelegate { @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } // MARK: - UITableViewDataSource func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10; } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: InputTextTableCell = tableView.dequeueReusableCellWithIdentifier("InputTextCell", forIndexPath: indexPath) as! InputTextTableCell cell.delegate = self return cell } // MARK: - InputTextTableCellDelegate func textFieldDidEndEditing(cell: InputTextTableCell, value: NSString) -> () { let path = tableView.indexPathForRowAtPoint(cell.convertPoint(cell.bounds.origin, toView: tableView)) NSLog("row = %d, value = %@", path!.row, value) } // MARK: - UITableViewDelegate func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return InputTextTableCell.height() } }
InputTextTableCell.swift
import UIKit protocol InputTextTableCellDelegate { func textFieldDidEndEditing(cell: InputTextTableCell, value: NSString) -> () } class InputTextTableCell: UITableViewCell, UITextFieldDelegate { override func awakeFromNib() { super.awakeFromNib() } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } static func height() -> CGFloat { return 75.0 } // MARK: - UITextFieldDelegate internal func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() return true } internal func textFieldDidEndEditing(textField: UITextField) { self.delegate.textFieldDidEndEditing(self, value: textField.text!) } }
ソースコードは以上で、2箇所にエラーが出てしまっています。
###該当のソースコード
まず、1箇所目はViewController.swiftの関数<tableView(...CellForRowAtIndexPath){...}>内の以下の部分です。
cell.delegate = self
2箇所目はInputTextTableCell.swiftの<internal func textFieldDidEndEditing>内の以下の部分です。
self.delegate.textFieldDidEndEditing(self, value: textField.text!)
2箇所とも、下記の表記でエラーが出ている状態です。
###エラーの内容
Value of type 'InputTextTableCell' has no member 'delegate'.
エラーの表記内容から、InputTextTableCellのdelegateの部分でエラーが起こってしまっているのかなと思うのですが、
改善の方法がわからず、ご質問した次第です。
どのように改善すればいいのかお分かりの方がいらっしゃったら、改善方法をご享受頂きたく存じます。
よろしくお願い申し上げます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/22 19:10 編集
2016/05/23 00:07
2016/05/23 05:12