UIPickerView 中显示的数组不正确

Posted

技术标签:

【中文标题】UIPickerView 中显示的数组不正确【英文标题】:incorrect array showing in UIPickerView 【发布时间】:2019-04-13 14:24:10 【问题描述】:

我有两个 UIPickerviews 出现在一个 ViewController 上。我已尝试关注以下tutorial。

我已按照建议标记了每个 UITextFields(我总共有 4 个)。看图。

下面是 UIPickerViews 的代码。

extension DriverViewController: UIPickerViewDelegate, UIPickerViewDataSource 
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        if pickerView.tag == 0 
            return tracks.count
         else 
            return drivers.count
        
    

    func numberOfComponents(in pickerView: UIPickerView) -> Int 
        return 1
    

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
        if pickerView.tag == 0 
            return "\(tracks[row])"
         else 
            return "\(drivers[row])"
        
    

我遇到的问题是轨道数组出现在所有 UITextfields 上。

下面是每个数组的代码和 UIPickerView 的标题。

override func viewDidLoad() 
    super.viewDidLoad()
    tracks = ["Melbourne", "Manama", "Shanghai", "Baku",
              "Barcelona", "Monaco", "Montreal","Le Castellet","Spielberg",
              "Silverstone","Hockenheim","Budapest","Francorchamps","Monza","Singapore","Sochi","Suzuka","Austin","Interlagos","Abu Dhabi"]
    drivers = ["Lewis Hamilton","Antonio Giovinazzi","Kimi Raikkonen","Charles Leclerc","Sebastian Vettel","Romain Grosjean","Kevin Magnussen","Lando Norris",
               "Carlos Sainz","Valtteri Bottas","Sergio Perez","Lance Stroll","Pierre Gasly","Max Verstappen","Nico Hulkenberg","Daniel Ricciardo","Alexander Albon","Daniil Kvyat","Robert Kubica","George Russell"]
    createTrackPicker()
    createDriverPicker()
    createToolBar()
    // Do any additional setup after loading the view.


func createTrackPicker() 
    let trackPicker = UIPickerView()
    trackPicker.delegate = self
    TrackTextField.inputView = trackPicker


func createDriverPicker() 
    let driverPicker = UIPickerView()
    driverPicker.delegate = self
    firstTextField.inputView = driverPicker
    secondTextField.inputView = driverPicker
    thirdTextField.inputView = driverPicker


func createToolBar() 
    let toolBar = UIToolbar()
    toolBar.sizeToFit()
    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.dismissKeyboard))
    toolBar.setItems([doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true
    TrackTextField.inputAccessoryView = toolBar
    firstTextField.inputAccessoryView = toolBar
    secondTextField.inputAccessoryView = toolBar
    thirdTextField.inputAccessoryView = toolBar


@objc func dismissKeyboard() 
    view.endEditing(true)

【问题讨论】:

【参考方案1】:

你忘记了标签。

func createDriverPicker() 
    let driverPicker = UIPickerView()
    driverPicker.tag = 1 // <—-

【讨论】:

以上是关于UIPickerView 中显示的数组不正确的主要内容,如果未能解决你的问题,请参考以下文章

记一次 UIPickerView 无法滚动的问题。

用UIPickerView来显示省和市

UIPickerView 空

UIPickerView 为空,数据源为 JSON 数组

UIPickerView 无法显示 JSON 数组数据

UIPickerView 未正确显示数据(仅显示“?”)