2016-10-06 10 views
0

私は、親クラスCreateEventから継承するサブクラスCustomCellを持っています。サブクラスは、CreateEvent Viewコントローラにあるテーブルビューセルの個々のセルを記述します。ある特定のセルでは、CustomCellファイルにリンクされているテキストフィールドがありますが、ユーザーがテキストフィールドに入ると、そのテキストフィールドから値を取得するのに問題があります。私はまた、外のタッチとリターンキーを押すと、キーボードを却下する問題がありますが、私は主にテキストフィールドからテキストを取得に焦点を当てています。私はこれらの機能を通常の迅速なファイルで行うことに精通していますが、これはサブクラスなので、何をすべきかはわかりません。私が試したことは使用することです:テーブルビューのセル(テキストフィールド付き)

class CustomCell: UITableViewCell, UITextFieldDelegate { 

@IBOutlet weak var entranceFeeTextField: UITextField! 

override func awakeFromNib() { 
    super.awakeFromNib() 

} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 
} 

そして:

class CreateEventVC: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomCellDelegate, UITextFieldDelegate { 

override func viewDidLoad() { 
} 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let currentCellDescriptor = getCellDescriptorForIndexPath(indexPath) 
    let cell = tableView.dequeueReusableCell(withIdentifier: currentCellDescriptor["cellIdentifier"] as! String, for: indexPath) as! CustomCell 


    cell.entranceFeeTextField.delegate = self 

    entranceFeeAmount = cell.entranceFeeTextField.text! 
} 

このコードが実行されないと、私は私ができるようにするために実行する必要が代表をテキストフィールドを正確にわからないんだけどテキストフィールドからText値を取得します。

答えて

1

UITextFieldDelegateの方法textFieldShouldEndEditing(:)またはtextFieldShouldReturn(:)を使用して、テキストフィールドの結果を取得できます。例えば

:このコードスニペットで

func textFieldShouldEndEditing(textField: UITextField) -> Bool { 
    print("TextField should end editing method called") 
    let textFromCell = textField.text! 
    //do whatever you want with the text! 
    return true; 
} 

textFieldは実際にentranceFeeTextFieldのインスタンスになります。そのテキストフィールドの編集が終了すると、self.delegate?.textFieldShouldEndEditing(entranceFeeTextField)が呼び出され、そのメソッドのの実装CreateEventVCの中にあります。

trueを返すと、テキストフィールドの編集が終了します。このメソッドは、ユーザーが編集を停止したいときにのみ呼び出されます。そのため、cellForRowAtIndexPathメソッドからentranceFeeAmount = cell.entranceFeeTextField.text!を削除する必要があります。これは、そのセルを作成する場所です。その時点で、ユーザーはテキストフィールドに入力していないので、テキストが作成されるとすぐにそのテキストを取得することはありません。

あなたがしなければならないことは、これらの方法のうちの1つをCreateEventVCに実装することだけです。

+0

これは非常に便利です。テキストフィールドに値をプルできるように編集を停止するように指示する必要があります。しかし、 "let textFromCell = textField.text!"という行では、CustomCellファイルからtextFieldを認識しません。 – Kevin

+0

@Kevinデリゲートを適切に設定し、そのメソッドをあなたの 'CreateEventVC'に実装すれば、そうです。メソッドのパラメータには 'textField'と表示されますが、これはデリゲートによって渡されるtextFieldだけです。呼び出されると、あなたの 'entranceFeeTextField'になります。他に何かを意味するのでなければ? – Zolnoor

+0

私はentranceFeeTextFieldを使用しました。それは私があまりにも適切に適合している代議員でなければなりません。それは 'cell.entranceFeeTextField.delegate = self'ではありませんか?デリゲートを満たすために他に何が必要ですか?おかげで – Kevin

0

これは作業コードです。私はテキストフィールドから値を取得し、キーボードも辞めます。

var cell = TableViewCell() // customCell 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     cell = tbl.dequeueReusableCell(withIdentifier: "CELL") as! TableViewCell 
     cell.configure(text: "", placeholder: "EnterText") 
     return cell 
    } 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 
    return 1 
} 

func numberOfSections(in tableView: UITableView) -> Int 
{ 
    return 1 
} 

func textFieldShouldReturn(_ textField: UITextField) -> Bool 
{ 
    //cell = tbl.dequeueReusableCell(withIdentifier: "CELL") as! TableViewCell 


    print(cell.returnTextOfTextField()) 
    print(cell.txtField.text) 
    cell.txtField .resignFirstResponder() 
    return true 
} 




/// Custom cell class 


class TableViewCell: UITableViewCell,UITextFieldDelegate 
{ 

@IBOutlet weak var txtField: UITextField! 
override func awakeFromNib() 
{ 
    super.awakeFromNib() 
    // Initialization code 
} 
public func configure(text: String?, placeholder: String) { 
    txtField.text = text 
    txtField.placeholder = placeholder 

    txtField.accessibilityValue = text 
    txtField.accessibilityLabel = placeholder 
} 

func returnTextOfTextField() -> String 
{ 
    print(txtField.text) 
    return txtField.text! 
} 
override func setSelected(_ selected: Bool, animated: Bool) 
{ 
    super.setSelected(selected, animated: animated) 
    // Configure the view for the selected state 
} 

}

+0

'cell = tbl.dequeueReusableCell(withIdentifier: "CELL")というコードで問題があります。 TableViewCell cell.configure(テキスト: ""、プレースホルダ: "EnterText")return cell} 'ポップアップには多くのエラーがあります。私はwithIdentifier: "CELL"と同様に、プレースホルダーとも言われているのか分かりません: "EnterText"です。 – Kevin

+0

同じもののスナップショットを追加してください。 TableViewCellはカスタムセルのクラスです。または、私ができることはあなたにコードを送ることです。あなたにメールIDを追加すると、コードが投稿されます。 – Shemona

+0

https://github.com/shemona-ios/stack – Shemona

1
Here is the full code: (Xcode 8 swift 3) 
(View Controller Class) 

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate 
{ 
    @IBOutlet weak var tbl: UITableView! 
    var cell = TableViewCell() 
    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() 
    { 
     super.didReceiveMemoryWarning() 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     cell = tbl.dequeueReusableCell(withIdentifier: "CELL") as! TableViewCell 
     cell.configure(text: "", placeholder: "EnterText") 
     return cell 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return 1 
    } 

    func numberOfSections(in tableView: UITableView) -> Int 
    { 
     return 1 
    } 

    func textFieldShouldReturn(_ textField: UITextField) -> Bool 
    { 


     print(cell.returnTextOfTextField()) 
     print(cell.txtField.text) 
     cell.txtField .resignFirstResponder() 
     return true 
    } 

} 


TableViewCell class (Custom cell): 
class TableViewCell: UITableViewCell,UITextFieldDelegate 
{ 

    @IBOutlet weak var txtField: UITextField! 
    override func awakeFromNib() 
    { 
     super.awakeFromNib() 
     // Initialization code 
    } 
    public func configure(text: String?, placeholder: String) { 
     txtField.text = text 
     txtField.placeholder = placeholder 

     txtField.accessibilityValue = text 
     txtField.accessibilityLabel = placeholder 
    } 

    func returnTextOfTextField() -> String 
    { 
     print(txtField.text) 
     return txtField.text! 
    } 
    override func setSelected(_ selected: Bool, animated: Bool) 
    { 
     super.setSelected(selected, animated: animated) 
     // Configure the view for the selected state 
    } 

} 


"CELL" is the identifier given to cell in Nib . 
+0

私のコードを実行すると:cell = tableView.dequeueReusableCell(withIdentifier: "entranceFee")! CustomCell、ここでentranceFeeはテキストフィールドを含む私の.xibファイルです。コンパイラは「致命的なエラー:任意の値をアンラッピングしている間に予期せぬエラーが発生しました」と苦情を言います。 – Kevin

+0

コードを見つけるリンクを参照してください:\t https:// github。 com/shemona-ios/stack – Shemona

関連する問題