UIPickerView UITextField中显示多行

时间:2019-03-14 11:08:48

标签: swift uitextfield uipickerview

我试图在TextField中显示我的UIPickerView,我已经做到了,所以它显示了一行,但不是全部。

这是我的代码:

class ViewController: UIViewController {

static var isAlreadyLaunchedOnce = false
var ref: DatabaseReference!


    @IBOutlet weak var txtDate: UITextField!


    @IBOutlet weak var inputpickerdate: UIPickerView!



    var pickerDate: [[String]] = [[String]] ()


    var selected: String?

    override func viewDidLoad() {
        super.viewDidLoad()

    self.inputpickerdate.delegate = self
    self.inputpickerdate.dataSource = self

        pickerDate = [["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], ["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", "Sept", "Oct", "Nov", "Dec"]]

        if FirebaseApp.app() == nil {
            FirebaseApp.configure()
        }



}

extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {

    func numberOfComponents(in pickerView: UIPickerView) -> Int {

    return 3

    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            //return pickerList.count
        return pickerDate [component].count

        }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        return pickerDate [component] [row]

   }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        txtDate.text = pickerDate[component][row]
        self.view.endEditing(false)

    }

    }

在文本视图中,我只看到月份而不是完整日期,我想显示类似以下内容:

星期一年

不确定如何执行此操作,基本上需要显示所有行,而不仅仅是行。

1 个答案:

答案 0 :(得分:0)

原因是您仅将UITextField中的文本设置为要选择pickerDate[component][row]的组件的文本。

使用UIPicker可以实现多种方法,尽管相当复杂...

例如

  • 您可以有三个标签,每个组件一个。
  • 您可以持久选择每个组件,然后从持久值中构建一个字符串。

一种更好的选择日期的方法是改用UIDatePicker

class ViewController: UIViewController {

    @IBOutlet weak var txtDate: UITextField!
    @IBOutlet weak var datepicker: UIDatePicker!

    let dateFormatter = DateFormatter()

    override func viewDidLoad() {
        super.viewDidLoad()

        dateFormatter.dateStyle = .medium
        datepicker.addTarget(self, action: #selector(dateChanged(_:)), for: .valueChanged)
    }

    @objc func dateChanged(_ sender: UIDatePicker) {
        txtDate.text = dateFormatter.string(from: sender.date)
    }
}

如果您想遵循使用UIPickerView的方法,则可以保留每个组件的值,并使用这些值来构建要显示的字符串。

class ViewController: UIViewController {

    @IBOutlet weak var txtDate: UILabel!
    @IBOutlet weak var picker: UIPickerView!

    let pickerData: [[String]] = [
        ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
        ["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", "Sept", "Oct", "Nov", "Dec"]
    ]

    var weekDay: String = ""
    var dayOfMonth: String = ""
    var month: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        self.picker.delegate = self
        self.picker.dataSource = self

        // Give the label a default value
        weekDay = pickerData[0][0]
        dayOfMonth = pickerData[1][0]
        month = pickerData[2][0]

        updateDateText()
    }

    func updateDateText() {
        txtDate.text = "\(weekDay) \(dayOfMonth) \(month)"
    }
}

extension ViewController: UIPickerViewDataSource {

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return pickerData.count
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData [component].count
    }
}

extension ViewController: UIPickerViewDelegate {

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return pickerData[component][row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        switch component {
        case 0:
            weekDay = pickerData[component][row]
        case 1:
            dayOfMonth = pickerData[component][row]
        case 2:
            month = pickerData[component][row]
        default:
            break
        }

        updateDateText()
    }
}