2015-01-08 68 views
1

我試圖用2個UIPickerViews製作一個簡單的應用程序,當我放入第二個時,它只包含與第一個相同的數據。我已經聲明瞭功能將數據插入到第一個:超過1個UIPickerView Swift

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。

回答

7

您可以使用相同的代理方法來填充UIPickerView。只要根據你正在處理的UIPickerView有條件地填充它們。例如,如果你的第一個UIPickerView的叫firstPickerView和你的第二個UIPickerViewsecondDataSet填充:

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] 
    } 
}