如何根据Swift中另一个选择器视图的选择填充选择器视图?

时间:2015-06-30 08:05:23

标签: ios swift uipickerview

根据同一视图控制器中另一个选择器视图的选择,寻找填充选择器视图的解决方案。 第一个选择一个类别。第二个是该类别的项目。

这是我(Swift初学者)提出的:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource,      UIPickerViewDelegate {

var countryClasses = ["US", "EU", "JP"]
var usItems = ["usitem1", "usitem2", "usitem3", "usitem4", "usitem5"]
var euItems = ["euitem1", "euitem2", "euitem3", "euitem4", "euitem5"]
var jpItems = ["jpitem1", "jpitem2", "jpitem3", "jpitem4", "jpitem5"]

@IBOutlet weak var countryPicker: UIPickerView!

@IBOutlet weak var itemPicker: UIPickerView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.countryPicker.dataSource = self;
    self.countryPicker.delegate = self;
    self.itemPicker.dataSource = self;
    self.itemPicker.delegate = self;
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

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

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

}

3 个答案:

答案 0 :(得分:0)

将每个选择器视图与TAG关联(以便识别它们)然后在选择器委托中根据这些标识符识别这些选择器并显示其他标识符

答案 1 :(得分:0)

由于两个Pickers都使用相同的委托,因此当您的委托方法运行时,您需要一种方法来识别它们,您当前的Array结构意味着您将需要一种方法来选择每个项目数组。国家被选中。

为所选商品数组创建商店。

var selectedItemsArray = []

为您的委托方法添加一些检查。

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView == countryPicker {
        return countryClasses.count
    } else if pickerView == itemPicker {
        return selectedItemsArray.count
    }
    return 0
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    if pickerView == countryPicker {
        return countryClasses[row]
    } else if pickerView == itemPicker {
        return selectedItemsArray[row]
    }
    return 0
}

然后你需要实现选择行来设置项目存储并重新加载数据。

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView == countryPicker {
        switch row {
        case 0:
            selectedItemsArray = usItems
        case 1:
            selectedItemsArray = euItems
        case 2:
            selectedItemsArray = jpItems
        default:
            selectedItemsArray = []  
        }
        // IMPORTANT reload the data on the item picker
        itemPicker.reloadAllComponents()
    } else if pickerView == itemPicker {
        // Get the current item
        var item = selectedItemsArray[row]
        // Assign value to a label based on which array we are using
        if selectedItemsArray == usItems {
            usLabel.text = item
        } else if selectedItemsArray == euItems {
            euLabel.text = item
        } else if selectedItemsArray == jpItems {
            jpLabel.text = item
        }
    }
}

我还没有测试过这段代码,但逻辑应该是正确的。

答案 2 :(得分:0)

谢谢@Wez!

在Swift 2.2中,小但重要的校正返回函数,因为String使其工作得很好。

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    if pickerView == countryPicker {
        return countryClasses[row]
    } else if pickerView == itemPicker {
        return selectedItemsArray[row]
    }

    return selectedItemsArray[row] as? String
}