我尝试创建一个执行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
}
答案 0 :(得分: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 {
...
}
}