0

I'm trying to create a custom PickerView that gets it's data from an API call to a web-server. The problem I'm having is saving the parsed data into an external variable so that the PickerView protocol methods can access it.

// API Call / Parsing using Alamofire + Unbox
static func makeApiCall(completionHandler: @escaping (CustomDataStructure) -> ()) {
    Alamofire.request(webserverUrl, method: .get).responseObject { (response: DataResponse<Experiment>) in
        switch response.result {
            case .success:
                if var configParams = response.result.value {
                    let inputConfigs = removeExtrasParams(experiment: response.result.value!)
                    let modifiedViewModel = modifyViewModel(experiment: &configParams, inputConfigs: inputConfigs)
                    completionHandler(modifiedViewModel)
                }
            case .failure(_):
                break
        }
    }
}

// Custom PickerClass
class CustomPickerView: UIPickerView {
    fileprivate var customDS: CustomDataStructure?

    override init() {
        super.init()

        dataSource = self
        delegate = self

        SomeClass.makeApiCall(completionHandler: { customds in
            self.customDS = customds
        })
    }

    ...
}

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

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if let customds = customDS {
            if let customDSValues = customds.inputs.first?.value {
                return customDSValues[row]
            }
        }
        return "apple"
    }
}

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if let customds = customDS {
            return customds.inputs.values.count
        } else {
            return 0
        }
    }
}

The problem I'm having is that customDS returns nil everytime.

What am I doing wrong here?

4

1 回答 1

2

在完成块中,makeApiCall只需pickerView's在主线程上重新加载组件,就可以开始了。

SomeClass.makeApiCall(completionHandler: { customds in
    self.customDS = customds
    DispatchQueue.main.async {
        self.reloadComponent(0) 
    }
})
于 2017-05-16T16:00:37.457 回答