Múltiples columnas en "UIPickerView"

En mi proyecto se agregan Días y mes Cómo agregar dos campos en (date y hora) UIPickerView. En mi código es

class TableViewController: UITableViewController ,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource{ @available(iOS 2.0, *) func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } @IBOutlet var dateTextfield:UITextField! @IBOutlet var dateLabel:UILabel! var pickerDaysYear = [["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"],["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]]; override func viewDidLoad() { super.viewDidLoad() self.pickerview.delegate = self } func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 2 } numberOfRowsInComponent component: Int) -> Int { return self.pickerDaysYear[component].count } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { if(component == 0) { return self.pickerDaysYear[component][row] } else { return self.pickerDaysYear[component][row] } // return pickerDaysYear[component][row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { let days = pickerDaysYear[0][pickerView.selectedRow(inComponent: 0)] let month = pickerDaysYear[1][pickerView.selectedRow(inComponent: 1)] dateTextfield.text = days + " " + month } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(false) } 

La salida es:

[falta image]

Y esperaba esta salida:

introduzca la descripción de la imagen aquí

La function numberOfComponents y numberOfRowsInComponent son de origen de datos, no delegar.

Por lo tanto, necesita agregar esta línea a su viewDidLoad :

self.pickerview.delegate = self

 class TableViewController: UITableViewController ,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource{ @IBOutlet var dateTextfield:UITextField! @IBOutlet var dateLabel:UILabel! let datePicker = UIPickerView() override func viewDidLoad() { super.viewDidLoad() self.dateTextfield.delegate = self self.datePicker.dataSource = self self.datePicker.delegate = self self.dateTextfield.inputView = self.datePicker let toolBar = UIToolbar() toolBar.barStyle = UIBarStyle.default toolBar.isTranslucent = true toolBar.tintColor = UIColor(networking: 76/255, green: 217/255, blue: 100/255, alpha: 1) toolBar.sizeToFit() let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.doneButtonTapped)) toolBar.setItems([doneButton], animated: false) self.dateTextfield.inputAccessoryView = toolBar } func doneButtonTapped(){ self.dateTextfield.resignFirstResponder() } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //---------------- PickerView delegate Method --------------------------------// func numberOfComponents(in pickerView: UIPickerView) -> Int { return 2 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return self.pickerDaysmonth[component].count } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return self.pickerDaysmonth[component][row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { let days = pickerDaysmonth[0][pickerView.selectedRow(inComponent: 0)] let month = pickerDaysmonth[1][pickerView.selectedRow(inComponent: 1)] dateTextfield.text = days + " " + month } 

Asigne la vista de input para el campo de text en UIPickerView ()

 let datePicker = UIPickerView() self.dateTextfield.inputView = self.datePicker 

Asigne inputAccessoryView para campo de text en toolBar

 let toolBar = UIToolbar() self.dateTextfield.inputAccessoryView = toolBar 

Está funcionando bien

La salida actual es: introduzca la descripción de la imagen aquí