超过1个UIPickerView Swift

时间:2015-01-08 03:56:26

标签: ios swift uipickerview

我试图用2个UIPickerViews创建一个简单的应用程序,当我放入第二个时,它只包含与第一个相同的数据。我已经宣布了将数据插入第一个的功能:

var typesOfData = ["KB","MB","GB"]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return typesOfData.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return typesOfData[row]
}

但我需要知道如何为第二个UIPickerView声明这些函数。

1 个答案:

答案 0 :(得分:7)

您可以使用相同的委托方法填充UIPickerView。只需根据您UIPickerView处理的UIPickerView条件有条件地填充它们。例如,如果您的第一个firstPickerView被称为UIPickerView,而您的第二个secondDataSetvar typesOfData = ["KB","MB","GB"] var secondDataSet = ["A","B","C"] func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return 1 } func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { if pickerView == firstPickerView { return typesOfData.count } else { // if it's the second picker view return secondDataSet.count } } func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { if pickerView == firstPickerView { return typesOfData[row] } else { return secondDataSet[row] } } 填充:

{{1}}