如何将两个不同的UIPickerView数据添加到单个标签中?

时间:2019-03-11 05:24:05

标签: ios swift uipickerview

i have a two different UIPickerViews as shown image  left duration and right duration have same data with different arrays>如何将两个UIPickerView数据添加到单个标签中?如何在单个标签中添加小时和分钟?

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
    var hoursSelected1 = ""
    var minsSelected1 = ""

    var hoursSelected2 = ""
    var minsSelected2 = ""

    if strDurationComeFrom == "left"{

        hoursSelected1 = hoursArray1[myPickerView1.selectedRow(inComponent: 0)]
        minsSelected1 = minsArray1[myPickerView1.selectedRow(inComponent: 1)]
        leftDuration.text = "\(hoursSelected1) \(minsSelected1)"

    }

    else{

        hoursSelected2 = hoursArray2[myPickerView2.selectedRow(inComponent: 0)]
         minsSelected2 = minsArray2[myPickerView2.selectedRow(inComponent: 1)]



       rightDuration.text = "\(hoursSelected2) \(minsSelected2)"
    }
    totalDuration = leftDuration.text+rightDuration.text
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我将假设hoursArrayminsArray具有整数,如果没有,则将它们转换并在titleForRow中进行。我没有理由拥有minsArray1minsArray2。分钟会如何变化?时间如何不同?您只能有一个minsArray作为两个选择器的数据源。

if component == 0 {
    return "\(hoursArray[titleForRow]) h"
} else {
    return "\(minsArray[titleForRow]) m"
}

然后:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
    let hourSelected = hoursArray[row]
    let minSelected = minsArray[row]
    let otherHour: Int
    let otherMin: Int
    let otherPickerView: UIPickerView!
    let labelText = "\(hourSelected)h \(minSelected)m"

    if pickerView == pickerView1 {
        leftDuration.text = labelText
        otherPickerView = pickerView2
    } else {
        rightDuration.text = labelText
        otherPickerView = pickerView1
    }

    otherHour = hoursArray[otherPickerView.selectedRow(inComponent: 0)]
    otherMin = minsArray[otherPickerView.selectedRow(inComponent: 1)]

    totalDuration = "\(hoursSelected + otherHour)h \(minSelected + otherMin)m"

}

测试此代码将意味着重新生成整个设置,并且将花费很长时间,但是我确信逻辑可以正常工作。如果您有任何疑问,请告诉我。