Swift - 如何连接UIPickerView中的didSelectRow选项

时间:2016-09-12 18:43:51

标签: ios swift uipickerview

我有一个两列选择器(Miles和Tenths)

Picker似乎运行良好,但我似乎无法解决如何连接两列中的选择,即如果用户从第一列选择23,从第二列选择6,我想创建一个“23.6”的答案< / p>

目前,任一列选择都会覆盖先前的选择。

顺便说一句:我认为我不能追加两个字符串,因为如果用户先选择10个字符会怎么样?

    let resetPickerData = [Array(0...99), Array(0...9)]

// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
    return 2
}

// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
    return resetPickerData[component].count
}

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

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
    //var distance = ??
}

1 个答案:

答案 0 :(得分:3)

您需要像这样分别跟踪组件选择,然后将字符串整齐:

var firstComponentString = ""
var secondCompoentString = ""

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
    if component == 0 {
        firstComponentString = String(resetPickerData[component][row])
    }
    else  if component == 1 {
        secondCompoentString = String(resetPickerData[component][row])
    }

    var displayString = "\(firstComponentString).\(secondCompoentString)"
}