UIPIckerViews 在一个 UIView 中协同工作

Posted

技术标签:

【中文标题】UIPIckerViews 在一个 UIView 中协同工作【英文标题】:UIPIckerViews working together in one UIView 【发布时间】:2020-04-12 11:05:41 【问题描述】:

查看了其他类似的问题,但没有找到答案。整理了我遇到的一些问题,但仍然存在第一个 pickerView 中的数据确定第二个 pickerView 中有哪些选择的问题(应用程序的这一部分是单位转换器)。

我了解出了什么问题,但不知道如何解决问题。通过打印语句我可以看到,当我更改第一个pickerView 时,保存第二个pickerView 中的行数的值发生了变化。但是第二个 pickerView 标题不会改变,因此当我转到高于新数组中索引号的项目时,应用程序会崩溃。错误索引超出范围证实了这一点。

我已经包含了代码和 pickerViews 外观的快照(没有格式化或使其漂亮 - 喜欢首先获得功能)。

感谢您的帮助。

import UIKit

导入基础

类 ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate

var conversionTypes = ["length", "mass", "area", "volume", "rate", "temp", "pressure"]
var conversionItems = [["metres", "feet", "yard", "inch", "cm"], ["kg", "lbs", "tonne", "ounces"], ["ft2", "m2", "in2"], ["US Gall", "UK Gall", "Bbls", "ft3", "m3"], ["bbl/min", "scf/min", "scf/hr"], ["degC", "degF", "Kelvin"], ["bar", "psi"]]
var littlePickerType = 0
var wheelOne = 0
var wheelTwo = 0



@IBOutlet weak var numberToConvert: UITextField!
@IBOutlet weak var answerLabel: UILabel!


@IBOutlet weak var littlePicker: UIPickerView!
@IBOutlet weak var bigPicker: UIPickerView!


override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    littlePicker.delegate = self
    littlePicker.dataSource = self
    bigPicker.delegate = self
    bigPicker.dataSource = self

    let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing))
    view.addGestureRecognizer(tap)



func numberOfComponents(in pickerView: UIPickerView) -> Int 
    if pickerView == littlePicker 
        return 1
     else if pickerView == bigPicker 
        return 2
    

    return 1



func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    if pickerView == littlePicker 
        return conversionTypes.count
     else if pickerView == bigPicker 
        return conversionItems[littlePickerType].count
    

    return 1


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

    if pickerView == littlePicker 
        return conversionTypes[row]
     else if pickerView == bigPicker 
        return conversionItems[littlePickerType][row]
    
    return ""


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 

    if pickerView == littlePicker 
        littlePickerType = row
     else if pickerView == bigPicker 
        if component == 0 
            wheelOne = row
         else if component == 1 
            wheelTwo = row
        
    

    print ("Conversion Type \(littlePickerType) WheelOne \(wheelOne) WheelTwo \(wheelTwo) count \(conversionItems[littlePickerType].count)")



【问题讨论】:

【参考方案1】:

每当littlePickerType 更改时,您都应该在您的大选择器上调用reloadAllComponents。这会导致为大选择器再次调用所有选择器视图数据源方法(尤其是 titleForRow)。

var littlePickerType = 0 
    didSet 
        bigPicker.reloadAllComponents()
    

【讨论】:

非常感谢,这让我头疼了几个小时,检查了文档和谷歌,然后就在这里。

以上是关于UIPIckerViews 在一个 UIView 中协同工作的主要内容,如果未能解决你的问题,请参考以下文章

同一个 UIView 中的多个 UIPickerView

如何在一个视图控制器中使用两个 UIPickerViews?

一个 ViewController 中的三个 UIPickerViews

UIPickerViews 不显示

iPhone:多个 UIPickerViews

使用元组和 3 个 UIPickerviews 计算小计