2017-04-10 139 views
0

我在UIPickerView的viewForRow事件中嘗試了下面的代碼。當標籤出現時,Textfield不出現。是否允許在選擇器視圖中添加文本框?如何在UIPickerView中添加UITextField viewForRow

這是我的代碼:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { 
    var rowDict = NSDictionary() 
    var retView = UIView() 

    let label = UILabel(frame: CGRect.init(x: 0, y: 0, width: 400, height: 40)) 
    let textField = UITextField(frame: CGRect.init(x: 0, y: 0, width: 400, height: 40)) 

    if(pickerView == pickerView2) 
    { 
     rowDict = self.arrayList02[row] as! NSDictionary 
     let rowValue = rowDict["QueryAttribute"] as? String 
     label.numberOfLines = 0 
     label.text = rowValue 
     label.sizeToFit() 
     if rowValue == "Other Specify:"{ 
      textField.placeholderText = rowValue 
      textField.isUserInteractionEnabled = true 
      retView = textField 
     } 
     else { 
      retView = label 
     } 
    } 

    return retView 
} 
+0

你的代碼,用戶輸入指示了'viewForRow'可以選擇其中一個選項,一個UILabel或一個UITextField。可能您的rowValue從未等於「其他指定:」 – antonio081014

+0

我試過刪除UILabel。現在UITextField正在出現。但它似乎並不可編輯..不允許輸入文本。 –

+0

是否可以在UIPickerView中爲特定的行值添加額外的UITextField? –

回答

0

我想這個代碼採取的UIPickerView選擇特定選項

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
     selectionHandler?(pickerView, row, arrayList[row] as! String) 
     var rowValue = NSDictionary() 
     if(arrayList.count > 0) { 
      rowValue = self.arrayList[row] as! NSDictionary 
      if(rowValue["ItemCode"] as? String != "99"){ 
       textView.text = rowValue["Item"] as? String 
       textView.tag = rowValue["Id"] as! Int 
      } 

      if(isOtherSpecifyRequired){ 
       let lastRow = pickerView.numberOfRows(inComponent: component) - 1 
       let lastRowValue = rowValue["Item"] as? String 
       setOtherSpecifyField(lastRow: lastRow, row: row, lastRowValue: lastRowValue!, textField: textView) 
      } 
     } 
    } 

    func setOtherSpecifyField(lastRow: Int, row: Int, lastRowValue: String, textField: CustomTextField){ 
     if row == lastRow{ 
      ShowAlertWithTextBox(title: "Other Specify", message: "Please enter some text ", parentView: textField) 
      textField.tag = 0 
     } 
    } 

    func ShowAlertWithTextBox(title: String, message: String, parentView: UITextField) 
    { 
     let alert = UIAlertController(title: title , message: message, preferredStyle: UIAlertControllerStyle.alert) 
     alert.addTextField(configurationHandler: textFieldHandler) 
     alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil)) 
     alert.addAction(UIAlertAction(title: "Submit", style: UIAlertActionStyle.default, handler: { (UIAlertAction) in 
      parentView.text = alert.textFields?[0].text })) 
     if let presented = _viewController.presentedViewController { 
      presented.removeFromParentViewController() 
     } 
     if _viewController.presentedViewController == nil { 
      self._viewController.present(alert, animated: true, completion: nil) 
     } 
    } 

    func textFieldHandler(textField: UITextField!) 
    { 
     if (textField) != nil { 
      textField.placeholder = "Other Specify" 
     } 
    } 
相關問題