我是Iphone的新手,我正在开发一个必须使用UIPickerView的应用程序。我可以静态填充UIPickerView,但现在我必须从WEBSERVICES获取数据并将它们填充到Picker View中。
能告诉我一步一步的指导。
答案 0 :(得分:1)
为此,您可以使用UIPickerView
委托方法和一个MutableArray
使用MutableArray
存储您的网络服务响应,然后在UIPickerView
委托方法中使用它:
// number of component to display in the pickerview
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
// numbers of row is the total numbers of object store in your array
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return [YourArray count];
}
//display your title
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *obj = [YourArray objectAtIndex:row];
return obj;
}
// on click whatever you want to do.
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// onClick i am displaying the picker value in my textview you can do as per your requirement.
mytextview.text=[YourArray objectAtIndex:row];
}
答案 1 :(得分:0)
快捷键4
// MARK: - Picker View Delegate & Data Source
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return yourArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return yourArray[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
textView.text = yourArray[row]
}