2017-05-12 107 views
0

我正在嘗試創建一個框架,它執行API調用並使用API​​調用中填充的數據創建PickerView。 iOS客戶端將導入這個框架並調用一個公開的函數,該函數應該返回帶有加載數據的PickerView。可能創建一個返回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 
} 
+1

你應該繼承'UIPickerView'並且創建你自定義的類 – JuicyFruit

+0

這樣做的竅門,謝謝。 – manohjay

回答

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 { 
     ... 
    } 
} 
相關問題