可以创建 returns 带有数据的 PickerView 的函数吗?

Possible to create a function that returns PickerView with data?

我正在尝试创建一个框架,该框架执行 API 调用并使用来自 API 调用的填充数据创建一个 PickerView。 iOS 客户端将导入此框架并调用一个公开的函数,该函数应该 return 带有加载数据的 PickerView。

我设法创建了一个创建 PickerView 的函数,但不知道如何将数据插入 PickerView。

    // Framework Side

    public static func returnPickerView() -> UIPickerView {
    let apple = ["apple", "orange", "durian", "banana"]

    let customPicker = UIPickerView()

    customPicker.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
    customPicker.layer.borderColor = UIColor.black.cgColor
    customPicker.layer.borderWidth = 1

    return customPicker
}

这个问题的解决方案是创建自定义 class 就像 JuicyFruit 所说的那样。

class CustomPickerView: UIPickerView {  
    override init(frame: CGRect) {
        super.init(frame: frame)

        dataSource = self
        delegate = self
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

extension CustomPickerView: UIPickerViewDelegate {
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        ...
    }

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

extension CustomPickerView: UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        ...
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        ...
    }
}