1

我是 Iphone 新手,我正在开发一个必须使用 UIPickerView 的应用程序。我能够静态填充 UIPickerView,但现在我必须从 WEBSERVICES 获取数据并将它们填充到 Picker View 中。

你能告诉我一步一步的指导吗?

4

2 回答 2

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];

    }
于 2013-02-13T04:36:47.517 回答
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]
}
于 2018-07-09T13:09:15.230 回答