我试图用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声明这些函数。
答案 0 :(得分:7)
您可以使用相同的委托方法填充UIPickerView
。只需根据您UIPickerView
处理的UIPickerView
条件有条件地填充它们。例如,如果您的第一个firstPickerView
被称为UIPickerView
,而您的第二个secondDataSet
被var 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}}