假设我有3个采摘者,其中包含以下内容:
Restaurant{
[Menu]
name
}
Menu{
[Ingredients]
time
}
Ingredients{
...
}
如果对象发生变化,如何实现自动更新的选择器? (例如,当餐厅被挑选时,其菜单和成分会发生变化,当选择菜单时,成分会发生变化)
答案 0 :(得分:1)
您可以这样做:
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
{
@IBOutlet weak var restaurantsPicker: UIPickerView!
@IBOutlet weak var menuPicker: UIPickerView!
@IBOutlet weak var ingredientsPicker: UIPickerView!
var restaurants = ["R1", "R2", "R3"]
var menus = ["R1": ["M1", "M2"],
"R2": ["M2"],
"R3": ["M1", "M3"]]
var ingredients = ["M1":["I1", "I2", "I3"],
"M2":["I1", "I3"],
"M3":["I2", "I3"]]
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
if pickerView === restaurantsPicker
{
return restaurants.count
}
else if pickerView === menuPicker
{
let restaurant = restaurants[restaurantsPicker.selectedRow(inComponent: 0)]
return menus[restaurant]!.count
}
else
{
let restaurant = restaurants[restaurantsPicker.selectedRow(inComponent: 0)]
let menu = menus[restaurant]![menuPicker.selectedRow(inComponent: 0)]
return ingredients[menu]!.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
if pickerView === restaurantsPicker
{
return restaurants[row]
}
else if pickerView === menuPicker
{
let restaurant = restaurants[restaurantsPicker.selectedRow(inComponent: 0)]
return menus[restaurant]?[row]
}
else
{
let restaurant = restaurants[restaurantsPicker.selectedRow(inComponent: 0)]
let menu = menus[restaurant]![menuPicker.selectedRow(inComponent: 0)]
return ingredients[menu]?[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if pickerView === restaurantsPicker
{
menuPicker.reloadComponent(0)
ingredientsPicker.reloadComponent(0)
}
else if pickerView === menuPicker
{
ingredientsPicker.reloadComponent(0)
}
}
}
<强>截图:强>