所以我的第一个实际代码进展缓慢。我的目标是创建一个具有40行输入的应用程序,每行两个文本框用于输入,每行一个标签用于输出。我希望每个文本框输入都有一个选择器来选择输入。每行都是相同的,即,一个文本框用于“商店”,一个文本框用于数量,一个标签用于重量。希望所附图片能更好地理解界面样式。
因为它是40行,每行有2个文本框,所以输入很多。我对专家的提问有两个方面。首先,下面显示的代码可以正常工作。它适用于第一行,但无法弄清楚如何使用选择器将数据输入到第二行,依此类推。
第二,您可以从我的代码中看到我正在将每条输入行分别添加到代码中。有更有效的方法吗?
class SecondViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
// Outlets for LHS Cabin Floor Shoring LINE ONE
@IBOutlet weak var fwdLHSCabinFloorShoringOne: UITextField!
@IBOutlet weak var fwdLHSCabinFloorShoringOneQty: UITextField!
@IBOutlet weak var fwdLHSCabinFloorShoringOneWeight: UILabel!
// Outlets for LHS Cabin Floor Shoring LINE TWO
@IBOutlet weak var fwdLHSCabinFloorShoringTwo: UITextField!
@IBOutlet weak var fwdLHSCabinFloorShoringTwoQty: UITextField!
@IBOutlet weak var fwdLHSCabinFloorShoringTwoWeight: UILabel!
// Pickerview Menu list for Stores and Quantity
let storesQty = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]
let storesList = ["SLDMB", "Switlik Liferaft", "Mass Casualty Liferaft"]
var pickerViewStoresList = UIPickerView()
var pickerViewQuantity = UIPickerView()
// Stores weights
var stores = [
"SLDMB":26,
"Switlik Liferaft":119,
"Mass Casualty Liferaft":110,
]
var currentTextField: UITextField?
override func viewDidLoad() {
super.viewDidLoad()
pickerViewStoresList.delegate = self
pickerViewStoresList.dataSource = self
pickerViewQuantity.delegate = self
pickerViewStoresList.dataSource = self
// Textbox setup for pickerview
// FWD LHS Cabin Floor Shoring LINE 1
fwdLHSCabinFloorShoringOne.inputView = pickerViewStoresList
fwdLHSCabinFloorShoringOne.textAlignment = .center
fwdLHSCabinFloorShoringOne.placeholder = "Select SAR Store"
fwdLHSCabinFloorShoringOneQty.inputView = pickerViewQuantity
fwdLHSCabinFloorShoringOneQty.textAlignment = .center
fwdLHSCabinFloorShoringOneQty.placeholder = "Q"
// FWD LHS Cabin Floor Shoring LINE 2
fwdLHSCabinFloorShoringTwo.inputView = pickerViewStoresList
fwdLHSCabinFloorShoringTwo.textAlignment = .center
fwdLHSCabinFloorShoringTwo.placeholder = "Select SAR Store"
fwdLHSCabinFloorShoringTwoQty.inputView = pickerViewQuantity
fwdLHSCabinFloorShoringTwoQty.textAlignment = .center
fwdLHSCabinFloorShoringTwoQty.placeholder = "Q"
}
// Picker Functions
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == pickerViewStoresList {
return storesList.count
} else {
return storesQty.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == pickerViewStoresList {
return storesList[row]
} else {
return storesQty[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == pickerViewStoresList {
fwdLHSCabinFloorShoringOne.text = storesList[row]
fwdLHSCabinFloorShoringOne.resignFirstResponder()
} else {
fwdLHSCabinFloorShoringOneQty.text = storesQty[row]
fwdLHSCabinFloorShoringOneQty.resignFirstResponder()
}
}
// Calculate and display Line Weight
@IBAction func textChanged(_ sender: Any) {
//Weight for LHS Cabin Floor Shoring Line 1
let fwdLHSCabinFloorShoringOneSelectedStoreWeight = stores[fwdLHSCabinFloorShoringOne.text!]
let fwdLHSCabinFloorShoringOneQtyValue = Int(fwdLHSCabinFloorShoringOneQty.text!)
fwdLHSCabinFloorShoringOneWeight.text = String(describing:
fwdLHSCabinFloorShoringOneSelectedStoreWeight! * (fwdLHSCabinFloorShoringOneQtyValue ?? 0))
//Weight for LHS Cabin Floor Shoring Line 2
let fwdLHSCabinFloorShoringTwoSelectedStoreWeight = stores[fwdLHSCabinFloorShoringTwo.text!]
let fwdLHSCabinFloorShoringTwoQtyValue = Int(fwdLHSCabinFloorShoringTwoQty.text!)
fwdLHSCabinFloorShoringTwoWeight.text = String(describing:
(fwdLHSCabinFloorShoringTwoSelectedStoreWeight ?? 0) * (fwdLHSCabinFloorShoringTwoQtyValue ?? 0))
}
}