gpt4 book ai didi

arrays - 使用元组和 3 个 UIPickerview 计算小计

转载 作者:行者123 更新时间:2023-11-30 12:57:52 25 4
gpt4 key购买 nike

我是 Swift 编程的初学者。我创建了 3 个选择器 View 并从元组中添加了一些值。我的挑战是我不知道如何将 double 与元组分开并计算数量小计。无论 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
}

最佳答案

元组中的元素可通过 .0.1 等访问。这是计算小计的方式:

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

关于arrays - 使用元组和 3 个 UIPickerview 计算小计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40205332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com