0

我正在尝试创建一个执行 API 调用并使用来自 API 调用的填充数据创建 PickerView 的框架。iOS 客户端将导入此框架并调用一个公开的函数,该函数应返回包含已加载数据的 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
}
4

1 回答 1

0

这个问题的解决方案是创建一个自定义类,就像 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 {
        ...
    }
}
于 2017-05-16T16:06:59.913 回答