ios 如何根据按钮按下重新加载/刷新 UIPickerView(使用新数据数组)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8598271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:59:33  来源:igfitidea点击:

How do I reload/refresh the UIPickerView (with new data array) based on button press?

iosobjective-ccocoa-touchuipickerview

提问by jim4iOS

enter image description here

在此处输入图片说明

IF I wanted a picker, for states/provinces, i haven't seen an example, but I mocked up shown above, a picker for US/Can/Mex. wondering can you dynamically switch the NSMutableArray for the UIPickerView and then have it somehow reload each time you click on US/Can/Mex buttons??? How do I go about doing this. What approach do I take. Looking for someone to point a beginner for a clue in the right direction.

如果我想要一个选择器,对于州/省,我还没有看到一个例子,但我模拟了上面显示的,一个用于美国/加拿大/墨西哥的选择器。想知道您是否可以为 UIPickerView 动态切换 NSMutableArray,然后每次单击 US/Can/Mex 按钮时都以某种方式重新加载它???我该怎么做。我采取什么方法。寻找某人为初学者指出正确方向的线索。

回答by vikingosegundo

You will have to implement the datasource and the delegate.

您必须实现数据源和委托。

once you press a button, set an array pointer to the appropriate array.

按下按钮后,将数组指针设置为适当的数组。

than call [thePicker reloadAllComponents];

比打电话 [thePicker reloadAllComponents];

-(IBAction) usButtonPressed:(id)sender{
    self.inputArray = self.usArray;
    [self.thePicker reloadAllComponents];
}

-(IBAction) mexicoButtonPressed:(id)sender{
    self.inputArray = self.mexicoArray;
    [self.thePicker reloadAllComponents];
}

the datasource methods:

数据源方法:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [self.inputArray count];
}

the delegate methods:

委托方法:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.inputArray objectAtIndex:row];
}

回答by Vinod Joshi

Swift: xcode 6.1

斯威夫特:xcode 6.1

// reload the component of picker (Used this inside the trigger action or button)

// 重新加载选择器的组件(在触发动作或按钮内使用)

[self.pickerControl reloadAllComponents];

回答by Juan Manuel

By calling

通过调用

thePicker.delegate = self;

again, I was able to force refresh the pickerview with new info

再次,我能够用新信息强制刷新选择器视图

回答by Enamul Haque

In swift 3,4,or 5 you just write in didSelectRow function like bellow

在 swift 3、4 或 5 中,您只需编写如下 didSelectRow 函数

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {


        self.view.endEditing(false)
        yourPickerView.selectRow(0, inComponent: 0, animated: true)
        yourPickerView.reloadAllComponents();

}

回答by Jeff John

In your button press action,

在你的按钮按下动作中,

[self.pickerView selectRow:0 inComponent:0 animated:YES];

回答by WrightsCS

Use multiple pickers, or use a switch block to determine which data should be loaded, and reload the picker.

使用多个选择器,或使用 switch 块来确定应该加载哪些数据,然后重新加载选择器。

回答by Sumit

Swift 3 & 4

斯威夫特 3 & 4

I had the same problem but no solution works,

我遇到了同样的问题,但没有解决方案,

then i use DispatchQueue it works

然后我使用 DispatchQueue 它工作

DispatchQueue.main.async(execute: {
        self.picker.delegate = self
        self.picker.dataSource = self
        if self.pickerData.count > 0 {
            self.pickerTextField.text = self.pickerData[0]
            self.pickerTextField.isEnabled = true
        } else {
            self.pickerTextField.text = nil
            self.pickerTextField.isEnabled = false
        }
    })