現在、UITextField
とUIPickerView
をcellForRowAt
にプログラムで作成しています。 UITextField
をIBOutlet
に接続すると、選択したUIPickerView
の値をdidSelectRow
に簡単に更新できます。UIPickerViewから入力を受け取るときにUITextFieldを更新するにはどうすればよいですか?
ただし、適用されるAutoLayout制約を使用してプログラムでプログラムを作成する必要があります(UITextField
)。
しかし、私はそれはそうのようなcellForRowAt
以内プログラムによって作成ですので、私はUIPickerView's
didSelectRow
機能で、それへの参照を持っていないよう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?
}
は、あなたのテーブルビューは静的です。入力テキストフィールドは常にセクション2、インデックス0にありますか?あなたのセルはUITableViewCellサブクラスか、単にUITableViewCellですか? – Simon