问题
Currently, I am programmatically creating a UITextField
and UIPickerView
within cellForRowAt
. I can easily update the selected UIPickerView
value within didSelectRow
if I connect my UITextField
to an IBOutlet
.
However, I require the UITextField
to be created programmatically with applied AutoLayout constraints.
However, I don't know how to update the UITextField
as I have no reference to it in the UIPickerView's
didSelectRow
function since it's created programmatically within cellForRowAt
like so:
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?
}
回答1:
You can get the cell for the corresponding index path, and find the textfield in that cell. You have 2 options for finding the textField. One would be to use tags, in case you had more than one UITextField
s in that cell, which you don't. So you can do it like this:
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
}
}
}
}
来源:https://stackoverflow.com/questions/43087400/how-to-update-a-uitextfield-when-receiving-input-from-a-uipickerview