我如何设置一个包含 4 个组件的 pickerView,Arry? Swift

How do I setUp a pickerView with 4 components, Arry? Swift

我试了很多,但它只显示“?”在四个组件的行中。 是否可以将 String 放在 Array Like 的第一个位置作为 4 个组件的标题?我有将数组中的所有整数更改为字符串的想法,但这会花费我太长时间...

let componentOne = [1...1000]
    let componentTwo = [1...59]
    let componentThree = [1...59]
    let componentFour = [1...3]
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 4
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return 1000
        }
        if component == 1 {
            return 59
        }
        if component == 2 {
            return 59
        }
        if component == 3 {
            return 3
        }
        return component
    }
        
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> Int {
        if component == 0 {
            let row = componentOne[row]
            return 1000
            }
            if component == 1 {
                let row = componentTwo[row]
                return 59
                }
                if component == 2 {
                    let row = componentThree[row]
                    return 59
                    }
                    if component == 3 {
                        let row = componentFour[row]
                        return 10
                        }
        return row
                    }
    ```

您不能只更改委托方法。这是 correct 一个:

func pickerView(_ pickerView: UIPickerView, 
                titleForRow row: Int, 
                forComponent component: Int) -> String?

您有:

func pickerView(_ pickerView: UIPickerView,
                titleForRow row: Int,
                forComponent component: Int) -> Int

无论如何您都必须 return 一个 String? -- 如果您将函数的 return 类型更改为 Int,则它不符合协议要求。

好处是您可以轻松地从 Int 生成 String。只需像这样使用字符串插值:

/// Inside pickerView(_:titleForRow:forComponent:)

let row = componentOne[row]
    return "\(1000)"
}