2017-03-29 8 views
0

現在、UITextFieldUIPickerViewcellForRowAtにプログラムで作成しています。 UITextFieldIBOutletに接続すると、選択したUIPickerViewの値をdidSelectRowに簡単に更新できます。UIPickerViewから入力を受け取るときにUITextFieldを更新するにはどうすればよいですか?

ただし、適用されるAutoLayout制約を使用してプログラムでプログラムを作成する必要があります(UITextField)。

しかし、私はそれはそうのようなcellForRowAt以内プログラムによって作成ですので、私はUIPickerView'sdidSelectRow機能で、それへの参照を持っていないようUITextFieldを更新する方法がわからない:

var testing: [String] = [] 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    // Dequeue the cell to load data 
    let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 

    if indexPath.section == 2 
    { 
     let wearsPickerView: UIPickerView = UIPickerView() 

     wearsPickerView.delegate = self 
     wearsPickerView.dataSource = self 

     let textField: UITextField = UITextField() 

     textField.translatesAutoresizingMaskIntoConstraints = false 

     textField.textColor = UIColor.black 

     textField.text = "Test" 

     textField.delegate = self 
     textField.inputView = wearsPickerView 

     cell.contentView.addSubview(textField) 

     let leadingConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: textField, attribute: NSLayoutAttribute.leading, multiplier: 1.0, constant: 0) 
     let trailingConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: textField, attribute: NSLayoutAttribute.trailing, multiplier: 1.0, constant: 0) 
     cell.contentView.addConstraint(leadingConstraint) 
     cell.contentView.addConstraint(trailingConstraint) 

     let topConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: textField, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0) 
     let bottomConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: textField, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0) 
     cell.contentView.addConstraint(topConstraint) 
     cell.contentView.addConstraint(bottomConstraint) 
    } 

    return cell 
} 

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

    return true 
} 

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
{ 
    return testing.count 
} 

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
{ 
    return testing[row] 
} 

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
{ 
    // How to update UITextField string? 

} 
+0

は、あなたのテーブルビューは静的です。入力テキストフィールドは常にセクション2、インデックス0にありますか?あなたのセルはUITableViewCellサブクラスか、単にUITableViewCellですか? – Simon

答えて

1

あなたがのために、細胞を得ることができます対応するインデックスパス、およびそのセルのテキストフィールドを検索します。 textFieldを見つけるための2つのオプションがあります。あなたが持っていないセルにUITextFieldが1つ以上ある場合に備えて、1つはタグを使用することです。したがって、次のようにすることができます:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    { 
    // choose the correct row. I've assumed you only have one row. 
    if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 2)) { 
     for subview in cell.subviews { 
     if let textField = subview as? UITextField { 
      textField.text = testing[row] 
      break 
     } 
     } 
    } 
    } 
+0

なぜそれが 'UITextField'であるかをチェックするときにifステートメントを入力しない – Pangu

+0

代わりにタグを使用していますが、同様の実装を提案しました – Pangu

+0

タグを使用しても、最終的に' UITextField' '.text'のような' UITextField'固有のメソッドとプロパティにアクセスします。 –