根据在其他组件中选择的值禁用选择器一个组件中的值

发布于 2024-10-04 04:07:29 字数 2346 浏览 2 评论 0原文

我正在尝试实现一个自定义选择器。它有 3 个组件。现在,我想根据所选的第一个组件值将第二个组件中的某些值灰显。我参考了很多网站,并尝试通过谷歌搜索来禁用这些值。(准确地说,类似于 uidatepicker 的东西,如果我们选择二月,29 和 30 将变灰。但我正在尝试在实现我自己的自定义选择器中执行此操作内容)。有人可以帮助我如何禁用自定义选择器中的值吗? 函数

[myPickerView selectRow:27 inComponent:1 animated:NO];

我也尝试使用基于 if 条件的 。它直接转到该值,但不会灰显不必要的值。

我的代码:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
 NSString *returnStr = @"";

 // note: custom picker doesn't care about titles, it uses custom views
 if (pickerView == myPickerView)
 {
  if (component == 0)
  {
   returnStr = [pickerViewArray objectAtIndex:row];
  }
  else if(component ==1)
  {
   returnStr = [pickerViewArray1 objectAtIndex:row];
  }
  else
  {
   returnStr = [pickerViewArray2 objectAtIndex:row];
  }
 }

 return returnStr;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
 CGFloat componentWidth = 0.0;

 if (component == 0)
 {
  componentWidth = 140.0;
 }// first column size is wider to hold names
 else if(component ==1)
 {
  componentWidth = 40.0;
 }// second column is narrower to show numbers
 else if(component == 2)
 {
  componentWidth = 100;
 }

 return componentWidth;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
 return 40.0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
 //return [pickerViewArray count];
 if (component == 0)
 {
  return [pickerViewArray count];
 }// first column size is wider to hold names
 else if(component ==1)
 {
  return [pickerViewArray1 count];
 }

 else
 {
  return [pickerViewArray2 count];
 }


}

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

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
 if (pickerView == myPickerView) // don't show selection for the custom picker
 {
  // report the selection to the UI label


  label.text = [NSString stringWithFormat:@"%@  %@  %@",
      [pickerViewArray objectAtIndex:[pickerView selectedRowInComponent:0]],
      [pickerViewArray1 objectAtIndex:[pickerView selectedRowInComponent:1]],[pickerViewArray2 objectAtIndex:[pickerView selectedRowInComponent:2]]];
 }
}

I am trying to implement a custom picker. It has 3 components. Now, I want to gray out some values in the second component based on the first component value selected. I have referred to many sites and tried googling out to disable the values.(To be precise, something like the uidatepicker where if we select feb, 29 and 30 will be grayed. But i am trying to do it in custom picker implementing my own contents). Could some one help me how to go about for disabling the values in custom picker? I tried using the function

[myPickerView selectRow:27 inComponent:1 animated:NO];

based on the if conditions also. It directly goes to the value, but doesn't gray out the unnecessary values.

My code:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
 NSString *returnStr = @"";

 // note: custom picker doesn't care about titles, it uses custom views
 if (pickerView == myPickerView)
 {
  if (component == 0)
  {
   returnStr = [pickerViewArray objectAtIndex:row];
  }
  else if(component ==1)
  {
   returnStr = [pickerViewArray1 objectAtIndex:row];
  }
  else
  {
   returnStr = [pickerViewArray2 objectAtIndex:row];
  }
 }

 return returnStr;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
 CGFloat componentWidth = 0.0;

 if (component == 0)
 {
  componentWidth = 140.0;
 }// first column size is wider to hold names
 else if(component ==1)
 {
  componentWidth = 40.0;
 }// second column is narrower to show numbers
 else if(component == 2)
 {
  componentWidth = 100;
 }

 return componentWidth;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
 return 40.0;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
 //return [pickerViewArray count];
 if (component == 0)
 {
  return [pickerViewArray count];
 }// first column size is wider to hold names
 else if(component ==1)
 {
  return [pickerViewArray1 count];
 }

 else
 {
  return [pickerViewArray2 count];
 }


}

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

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
 if (pickerView == myPickerView) // don't show selection for the custom picker
 {
  // report the selection to the UI label


  label.text = [NSString stringWithFormat:@"%@  %@  %@",
      [pickerViewArray objectAtIndex:[pickerView selectedRowInComponent:0]],
      [pickerViewArray1 objectAtIndex:[pickerView selectedRowInComponent:1]],[pickerViewArray2 objectAtIndex:[pickerView selectedRowInComponent:2]]];
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杀手六號 2024-10-11 04:07:29

我还没有尝试过,但是有 -pickerView:viewForRow:forComponent:reusingView:。它的文档说:

如果之前使用的视图(视图参数)足够,则返回该视图。如果返回不同的视图,则先前使用的视图将被释放。选择器视图将返回的视图置于行矩形的中心。

因此,如果我正确理解,您可以将传入的内容更改为灰色(灰色)并返回。

I haven't tried it, but there is -pickerView:viewForRow:forComponent:reusingView:. Its documentation says:

If the previously used view (the view parameter) is adequate, return that. If you return a different view, the previously used view is released. The picker view centers the returned view in the rectangle for row.

so if I undertstand corectly, you could take that passed in view, change it (grey out) and return it.

冷默言语 2024-10-11 04:07:29

您是否声明您的控制器符合头文件中的协议?您是否知道当您更改选择时是否会调用上面的委托方法,特别是 didSelectRow 方法?

要找出答案,只需在该方法上设置断点,构建并运行并尝试更改 pickerview 选择。

一旦您有了这个疑问,就需要分析选择并提取正确的数据源,然后重新加载组件,您似乎在上面的实现中缺少该组件:

[pickerView reloadComponent:(int)];

我希望它有所帮助。
干杯,
罗格

Have you declared that your controller conforms to the protocol in your header file? Do you know if the delegate methods above are being called when you change you selection, particularly the didSelectRow one?

To find out just set a breakpoint on that method, build and run and try changing the pickerview selection.

Once you have that sussed, it is matter of analysing the selection and pulling the correct data source, then reloading the component, which you seem to be missing from the implementation above:

[pickerView reloadComponent:(int)];

I hope it helps.
Cheers,
Rog

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文