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

Posted

技术标签:

【中文标题】使用元组和 3 个 UIPickerviews 计算小计【英文标题】:Calculate subtotal using tuples and 3 UIPickerviews 【发布时间】:2016-10-23 16:48:30 【问题描述】:

我是 Swift 编程的初学者。我创建了 3 个选择器视图并从元组中添加了一些值。我的挑战是我不知道如何将双打与元组分开并用数量计算小计。无论Picker2 上显示的票证类型是什么,我都需要将其乘以Picker3 的数量并显示小计。

这是我的代码。

import UIKit

class BuyTicketViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource, UITextFieldDelegate
    @IBOutlet weak var Picker1: UIPickerView! = UIPickerView()
    @IBOutlet weak var Picker2: UIPickerView! = UIPickerView()
    @IBOutlet weak var Picker3: UIPickerView! = UIPickerView()
    @IBOutlet weak var cityTextField: UITextField!
    @IBOutlet weak var typeTextField: UITextField!
    @IBOutlet weak var qtyTextField: UITextField!

    var seletedItemArray = [(String,Double)]()
    var Cities = ["Las Vegas","Los Angeles","New York"]
    var typeLasVegas = [("City Tour  $20.50", 20.50),("Museum  $10.00", 10.00), ("Amusement Park  $75.00",75.00)]
    var typeLosAnglese = [("City Tour $29.99", 29.99),("Museum $15.00", 15.00), ("Amusement Park $79.99",79.99)]
    var typeNewYork = [("City Tour $39.99", 39.99),("Museum $12.50", 12.50), ("Amusement Park $65.00",65.00)]

    var Qt = [("1", 1.0),("2", 2.0),("3",3.0),("4", 4.0),("5",5.0),("6",6.0)]

    var subTotal = 0.0
    var ticketQty = 0
    var ticketPrice = 0.0

    override func viewDidLoad() 
        super.viewDidLoad()

        Picker1.isHidden = true
        Picker2.isHidden = true
        Picker3.isHidden = true

        cityTextField.delegate = self
        Picker1.delegate = self
        typeTextField.delegate = self
        Picker2.delegate = self
        qtyTextField.delegate = self
        Picker3.delegate = self

        self.cityTextField.inputView = self.Picker1
        self.typeTextField.inputView = self.Picker2
        self.qtyTextField.inputView = self.Picker3

        Picker1.tag = 0
        Picker2.tag = 1
        Picker3.tag = 2
        // Do any additional setup after loading the view.
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
        if pickerView.tag == 0
        
            return Cities[row]
        
        else if pickerView.tag == 1
        
            return seletedItemArray[row].0
        
        else if pickerView.tag == 2
        
            return Qt[row].0
        
        return ""
    

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        if pickerView.tag == 0
        
            return Cities.count
        
        else if pickerView.tag == 1
        
            return seletedItemArray.count
        
        else if pickerView.tag == 2
        
            return Qt.count
        
        return 1
    

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    
        if pickerView.tag == 0
        
            switch row 
            case 0:
                seletedItemArray = typeLasVegas
            case 1:
                seletedItemArray = typeLosAnglese
            case 2:
                seletedItemArray = typeNewYork

            default:
                seletedItemArray = []
            
            Picker2.reloadAllComponents()
            cityTextField.text = Cities[row]
            Picker1.isHidden = true
            Picker2.isHidden = false
        
        else if pickerView.tag == 1
        
            let item = seletedItemArray[row].0

            if seletedItemArray[row] == typeLasVegas[row]
            
                typeTextField.text = item

             else if seletedItemArray[row] == typeLosAnglese[row]
            
                typeTextField.text = item
            else if seletedItemArray[row] == typeNewYork[row]
            
                typeTextField.text = item
            

            Picker2.isHidden = true
        
        else if pickerView.tag == 2
        
            qtyTextField.text = Qt[row].0
            Picker3.isHidden = true
        
    

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool 
        if textField == cityTextField
        
            self.Picker1.isHidden = false
            typeTextField.text = ""
            qtyTextField.text = ""
        
        else if textField == typeTextField
        
            Picker2.isHidden = false
        
        else if textField == qtyTextField
        
            Picker3.isHidden = false
        
        return false
    

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

【问题讨论】:

【参考方案1】:

元组中的元素以.0.1 等形式访问。这就是您计算subtotal 的方式:

let picker2row = Picker2.selectedRow(inComponent: 0)
let picker3row = Picker3.selectedRow(inComponent: 0)

let subtotal = selectedItemArray[picker2row].1 * Qt[picker3row].1

如果你使用命名元组

var selectedItemArray = [(title: String, price: Double)]()
var typeLasVegas: [(title: String, price: Double)] = [("City Tour  $20.50", 20.50),("Museum  $10.00", 10.00), ("Amusement Park  $75.00",75.00)]
var Qt: [(title: String, quantity: Double)] = [("1", 1.0),("2", 2.0),("3",3.0),("4", 4.0),("5",5.0),("6",6.0)]

那就更好了:

let subtotal = selectedItemArray[picker2row].price * Qt[picker3row].quantity

【讨论】:

以上是关于使用元组和 3 个 UIPickerviews 计算小计的主要内容,如果未能解决你的问题,请参考以下文章

Python元组和序列区别是啥?

UICollectionViewCells 中的 UIPickerViews 中的 3 个 UILabel 之间不需要的交互

python学习:元组和嵌套

python0.7-----元组和字典

tuple

2-python元组和列表