0

我有一个UIPickerView包含两个组件的组件,当我更改component[0] the data of the组件 [1] 时会重新加载。

现在我试图在一个字符串中获取两个组件的值,didSelectRow但是,我无法得到我想要的结果。

public func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if component == 0 {

            String0 = arr0[row]
            pickerView.reloadComponent(1)

        }
        if component == 1{

        String1 = arr1[row]

        }

        print("\(String0), \(String1)")

        // result: " , string1.value"
    }

当我选择component[0]结果更改但仅用于String0 我想要实现的是得到"string0.value, string1.value"

4

1 回答 1

2

您可以使用方法selectedRowInComponent获取每个组件中的选定行,选定的行将是您形成字符串的正确索引。

尝试:

public func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if component == 0 {
        pickerView.reloadComponent(1)

    }
    String0 = arr0[pickerView.selectedRowInComponent(0)]
    String1 = arr1[pickerView.selectedRowInComponent(1)]

    print("\(String0), \(String1)")

    // result: " , string1.value"
}
于 2016-03-14T22:59:14.800 回答