Swift中的多个pickerview didSelectRow错误

Posted

技术标签:

【中文标题】Swift中的多个pickerview didSelectRow错误【英文标题】:multiple pickerview didSelectRow error in Swift 【发布时间】:2020-04-23 19:19:37 【问题描述】:

我有 2 个选择器视图,1 个选择器视图用于从数组中选择一种加密货币,另一个选择器视图用于从数组中选择一种货币。我遇到的问题是,当我在模拟器上运行应用程序并在 cryptoPicker 中选择加密货币时,该应用程序还会从 currencyPicker 中选择相同的数组值,反之亦然。除非用户选择,否则我不希望从两个数组中提取 0 的数组值。

@IBOutlet weak var cryptoCurrentRate: UILabel!
@IBOutlet weak var currencyLabel: UILabel!
@IBOutlet weak var currencyPicker: UIPickerView!
@IBOutlet weak var cryptoPicker: UIPickerView!
@IBOutlet weak var cryptoLabel: UILabel!


var coinManager = CoinManager()


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


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


    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        if pickerView == currencyPicker 
        return coinManager.currencyArray.count
        
        if pickerView == cryptoPicker 
        return coinManager.cryptoArray.count
    
        return 0
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
        if pickerView == currencyPicker 
        return coinManager.currencyArray[row]
        
        if pickerView == cryptoPicker 
        return coinManager.cryptoArray[row]
    
        return ""
    

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
        let selectedCurrency = coinManager.currencyArray[row]
        let selectedCrypto = coinManager.cryptoArray[row]
        coinManager.fetchCryptoCoin(assetIdBase: selectedCrypto, assetIdQuote: selectedCurrency)
    

【问题讨论】:

【参考方案1】:

对picker和'didSelectRow'做同样的检查,你可以这样做:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
     var selectedCurrency: CurrencyModel? = nil
     var selectedCrypt: CryptModel? = nil


    if pickerView == currencyPicker 
        selectedCurrency = coinManager.currencyArray[row]
    
    if pickerView == cryptoPicker 
        selectedCrypto = coinManager.cryptoArray[row]
    

    coinManager.fetchCryptoCoin(assetIdBase: selectedCrypt == nil ? coinManager.cryptoArray[.zero] : selectedCrypt!, assetIdQuote: selectedCurrency == nil ? coinManager.currencyArray[.zero] : selectedCurrency!)

默认情况下,它会从 'coinManager.currencyArray' 或 'coinManager.cryptoArray' 中获取第一个值

【讨论】:

抱歉回复晚了,我试过了,但最终还是出现了在每个选择器上显示同一行的错误。因此,如果在选择器 1 上选择了第 1 行,那么它也在选择器 2 上被选择。我最终只是将 selectedCurrency 和 selectedCrypt 都放在了 1 个选择器(同一个选择器)上,因为这种方法更容易实现我的目标。但我想知道如何为可能的未来项目做到这一点。

以上是关于Swift中的多个pickerview didSelectRow错误的主要内容,如果未能解决你的问题,请参考以下文章

如何在 PickerView 中限制多个文件选择

具有多个文本字段输入视图的多个选取器视图 Swift

如何在 Swift 中的 if/else 语句中获取数据

Swift 3.0 多个选择器视图无法识别数据源变量

检查pickerView SwiftUi中的选择

SWIFT 核心数据加载到pickerview