我对swift中的拣货员有疑问。刚开始在IOS编程,我没有Objective-C的经验。我们必须为项目计算(总体拥有成本),目前我正在尝试获取拣货员的信息。
问题是:我已经制作了多个数组,并且 var segment 位于第一个选择器中。但是如果你选择第一个索引“A”它应该显示数组carA,如果我选择段“B”它应该显示carB数组。
这是我的代码,直到现在!我希望有人可以帮助我:)。
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
var segment = ["A","B","C","D","E"]
var kilometers = ["24000","36000","48000"]
var looptijd = ["2 jaar","3 jaar","5 jaar"]
var carA = ["Toyota Aygo","Suzuki Swift"]
var carB = ["Prius","Peugeot 207"]
var carC = ["Lexus ch270","audi a3"]
var carD = ["Lexus..","BMW X6"]
@IBOutlet weak var segmentLabel: UILabel!
@IBOutlet weak var kilometersLabel: UILabel!
@IBOutlet weak var looptijdLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
segmentLabel.text = segment[0]
kilometersLabel.text = kilometers[0]
looptijdLabel.text = looptijd[0]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 4
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (component == 0 ) {
return segment.count
} else {
if(component == 1) {
return kilometers.count
}
if(component == 2) {
return looptijd.count
}
if(component == 3) {
return carA.count
}
}
return component
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if (component == 0 ) {
return segment[row]
} else {
if(component == 1) {
return kilometers[row]
}
if(component == 2) {
return looptijd[row]
}
if(component == 3){
return carA[row]
}
return nil // ARE YOU SURE RETURN IT IS NO AFTER CURLY BRACKETS ?
} // THATS WHY YOU SHOULD BETTER INDENT YOUR CODE
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if(component == 0){
var segmentSelected = segment[row]
segmentLabel.text = segmentSelected
} else {
if(component == 1) {
var kilometersSelected = kilometers[row]
kilometersLabel.text = kilometersSelected
}
if(component == 2){
var looptijdSelected = looptijd[row]
looptijdLabel.text = looptijdSelected
}
}
}
}
答案 0 :(得分:2)
在这里,您需要再创建一个字符串数组。此阵列可以保留选择,然后在更改选定的第一个组件时重新加载组件。
首先它应该是CarA
。因此,该数组的默认选择将是第一个数组,然后在更改组件0行值时更改此数组值。
您需要在pickerView(_:didSelectRow:InComponent:)
方法中重新分配选择数组值,然后在重新加载后,所有选择器组件都可以完成。
下面的代码对你有用我已经测试过,重新实现了你的逻辑,如果它不起作用,请留下评论,我会为你提供演示。
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var picker: UIPickerView!
var segment: [String]!
var kilometers: [String]!
var looptijd: [String]!
var carA: [String]!
var carB: [String]!
var carC: [String]!
var carD: [String]!
var selectionArray: [String]!
@IBOutlet weak var segmentLabel: UILabel!
@IBOutlet weak var kilometersLabel: UILabel!
@IBOutlet weak var looptijdLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
segment = ["A","B","C","D","E"]
kilometers = ["24000","36000","48000"]
looptijd = ["2 jaar","3 jaar","5 jaar"]
carA = ["Toyota Aygo","Suzuki Swift"]
carB = ["Prius","Peugeot 207"]
carC = ["Lexus ch270","audi a3"]
carD = ["Lexus..","BMW X6"]
selectionArray = carA
segmentLabel.text = segment[0]
kilometersLabel.text = kilometers[0]
looptijdLabel.text = looptijd[0]
picker.delegate = self
picker.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 4
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (component == 0 ) {
return segment.count
} else {
if(component == 1) {
return kilometers.count
}
if(component == 2) {
return looptijd.count
}
if(component == 3) {
return carA.count
}
}
return component
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if (component == 0) {
return segment[row]
} else {
if(component == 1) {
return kilometers[row]
}
if(component == 2) {
return looptijd[row]
}
if(component == 3){
return selectionArray[row]
}
return nil // ARE YOU SURE RETURN IT IS NO AFTER CURLY BRACKETS ?
} // THATS WHY YOU SHOULD BETTER INDENT YOUR CODE
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (component == 0) {
var selectedString = segment[row];
segmentLabel.text = selectedString
if (selectedString == segment[0]) {
selectionArray = carA
} else if (selectedString == segment[1]) {
selectionArray = carB
} else if (selectedString == segment[2]) {
selectionArray = carC
} else if (selectedString == segment[3]) {
selectionArray = carD
}
picker.reloadAllComponents()
} else {
if(component == 1) {
var kilometersSelected = kilometers[row]
kilometersLabel.text = kilometersSelected
}
if(component == 2){
var looptijdSelected = looptijd[row]
looptijdLabel.text = looptijdSelected
}
}
}
deinit {
self.picker.delegate = nil
self.picker.dataSource = nil
}
}