현재 프로그래밍 방식으로 UITextField
및 UIPickerView
을 cellForRowAt
내에 만듭니다. 내 UITextField
을 IBOutlet
에 연결하면 didSelectRow
내에서 선택한 UIPickerView
값을 쉽게 업데이트 할 수 있습니다.UIPickerView에서 입력을받을 때 UITextField를 업데이트하는 방법은 무엇입니까?
그러나 적용된 AutoLayout 제약 조건을 사용하여 프로그래밍 방식으로 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