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

时间:2016-10-23 16:48:31

标签: arrays swift tuples uipickerview

我是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 :(得分:0)

来自元组的元素可以.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