我正在尝试实现自定义选择器。它有3个组件。现在,我想根据所选的第一个组件值在第二个组件中灰显一些值。我已经提到了许多网站,并尝试使用Google搜索来禁用这些值。(确切地说,像uidatepicker这样的东西,如果我们选择feb,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]]];
}
}
答案 0 :(得分:1)
我没有尝试过,但有-pickerView:viewForRow:forComponent:reusingView:
。它的文件说:
如果先前使用的视图(视图参数)足够,则返回该视图。如果返回其他视图,则会释放先前使用的视图。选择器视图将返回的视图居中放置在行的矩形中。
所以,如果我认真地对待,你可以把它传递到视野中,改变它(灰色)然后返回它。
答案 1 :(得分:0)
您是否声明您的控制器符合头文件中的协议?您是否知道在更改选择时是否正在调用上述委托方法,尤其是didSelectRow
?
要找出只在该方法上设置断点,构建并运行并尝试更改选择器视图选择。
一旦你有了这个问题,就需要分析选择并拉出正确的数据源,然后重新加载组件,你似乎在上面的实现中遗漏了这个:
[pickerView reloadComponent:(int)];
我希望它有所帮助。 干杯, ROG