ios uiview 如何刷新_ios-如何根据按钮按下方式重新加载/刷新UIPickerView(带有新的数据数组)?...

ios-如何根据按钮按下方式重新加载/刷新UIPickerView(带有新的数据数组)?

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

7个解决方案

116 votes

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

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

比通话[thePicker reloadAllComponents];

-(IBAction) usButtonPressed:(id)sender{

self.inputArray = self.usArray;

[self.thePicker reloadAllComponents];

}

-(IBAction) mexicoButtonPressed:(id)sender{

self.inputArray = self.mexicoArray;

[self.thePicker reloadAllComponents];

}

数据源方法:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

return 1;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

return [self.inputArray count];

}

委托方法:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

return [self.inputArray objectAtIndex:row];

}

vikingosegundo answered 2019-11-11T04:02:45Z

18 votes

斯威夫特:xcode 6.1

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

self.pickerControl.reloadAllComponents();

Vinod Joshi answered 2019-11-11T04:03:16Z

6 votes

通过致电

thePicker.delegate = self;

再次,我能够用新信息强制刷新pickerview

Juan Manuel answered 2019-11-11T04:03:47Z

3 votes

在xcode-8中的swift 3中,您只需像下面这样写出didSelectRow函数

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

self.view.endEditing(false)

yourPickerView.selectRow(0, inComponent: 0, animated: true)

yourPickerView.reloadAllComponents();

}

Enamul Haque answered 2019-11-11T04:04:10Z

2 votes

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

WrightsCS answered 2019-11-11T04:04:35Z

2 votes

在您的按钮按下动作中,

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

Jeff John answered 2019-11-11T04:05:02Z

0 votes

斯威夫特3和4

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

然后我使用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

}

})

Sumit answered 2019-11-11T04:05:34Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值