【问题标题】:Correct work with UIPickerVIew with two numbers components正确使用带有两个数字组件的 UIPickerVIew
【发布时间】:2020-04-13 16:14:47
【问题描述】:

我想在第一个组件中选择五个(例如)并希望第二个组件自动显示从第一个组件中选择的数字+1(即六个),并将第二个组件退休数字到五个(或其他,如果我选择)。

我怎样才能正确地做到这一点?

现在我的代码是空的:

var numbers: [Int] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 2
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if component == 0 {
        return numbers.count
    } else {
        return numbers.count
    }
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if component == 0 {
        return "\(numbers[row])"
    } else {
        return "\(numbers[row])"
    }
}

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

}

【问题讨论】:

    标签: ios swift xcode uipickerview


    【解决方案1】:

    如果我正确理解您的问题,可能会是这样

     func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
         if component == 0 {
            componentOne = numbers.filter { (a) -> Bool in
                return a > numbers[row]
            }
            pickerView.reloadComponent(1)
            pickerView.selectRow(0, inComponent: 1, animated: true)
        }
     }
    

    并在 dataSource 方法中使用 componentOne。

    我不确定在极端情况下你必须做什么,但你可以选择。

    【讨论】:

    • 从第一个组件中选择数字后,如何从第二个组件中删除数字?
    • pickerView.reloadComponent(1) 但您需要更改组件列表:)
    • @ДмитрийДеникаев 我更新了答案,尝试使用新代码并为第二个组件使用另一个数字列表,是否有一些选择需要更新第二个数组并重新加载组件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    相关资源
    最近更新 更多