disabling the values in one component of picker based on value selected in other component

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 13:48:54

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/4231084/disabling-the-values-in-one-component-of-picker-based-on-value-selected-in-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!