2017-03-29 6 views
0

현재 프로그래밍 방식으로 UITextFieldUIPickerViewcellForRowAt 내에 만듭니다. 내 UITextFieldIBOutlet에 연결하면 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? 

} 
+0

은 정적보기입니다. 입력 텍스트 필드는 항상 섹션 2, 인덱스 0에 있습니까? 귀하의 셀은 UITableViewCell 하위 클래스 또는 단순히 UITableViewCell입니까? – Simon

답변

1

은 당신이 세포를 얻을 수 있습니다 :

그러나, 나는 그것과 같이 cellForRowAt 내 프로그램 작성 이후 나는 UIPickerView'sdidSelectRow 기능에에 대한 참조가 없기 때문에 UITextField를 업데이트하는 방법을 모른다 해당 색인 경로를 찾고 해당 셀의 텍스트 필드를 찾으십시오. textField를 찾는 데는 두 가지 옵션이 있습니다. 하나는 그 셀에 UITextField이 하나 이상있는 경우를 대비하여 태그를 사용하는 것입니다. 이렇게하면 다음과 같이 할 수 있습니다.

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

왜 그런지는 모르겠지만 if 문이'UITextField '인지를 확인할 때 입력하지 않습니다. – Pangu

+0

대신 태그를 사용하여 알아 냈습니다. – Pangu

+0

태그를 사용하더라도 궁극적으로는'UITextField' '.text'와 같은'UITextField' 특정 메소드와 속성에 접근합니다. –