如何制作只有 3 个可见行的 uipickerview?像这样:
问问题
6818 次
5 回答
2
您必须以编程方式添加 UIDatePicker 才能显示三行。根据您的需要调整选择器大小。
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.frame = CGRectMake(0, 160, 320, 160);
datePicker.datePickerMode=UIDatePickerModeDate;
[self.view addSubview:datePicker];
于 2013-06-27T13:01:13.893 回答
2
我注意到大多数回复都提到了UIDatePicker或Components的数量,这两个似乎都不正确,因为问题专门询问了UIPickerView和Rows Visible的数量。
我通过实施找到了解决方案:
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return (80.0);
}
这允许您使行的高度更大(或更小),以便在控件中显示更少(或更多)行,尽管控件的垂直大小是相同的。
我确实相信可以更改控件的大小以避免所有多余的间距,但我还没有解决这个问题。
行高 25.0
行高 80.0
于 2013-07-22T06:32:52.833 回答
2
除了@GRW 的回答。因此,您不能为选择器视图设置任意高度;但您可以:将行高更改为 3 个可见行;并应用仿射变换来缩小整个选择器视图。
要保持纵横比:
- 您不仅必须缩放y,还必须使用相同的比例因子缩放x ;
- 将 -pickerView:widthForComponent: 除以比例因子;
- 将 -pickerView:rowHeightForComponent: 除以比例因子;
- 如果组件行中有标签/文本,则字体大小也必须除以因子。
// consider CGFloat _scaleFactor = 0.5; - (void)viewDidLoad { [super viewDidLoad]; self.pickerView.transform = CGAffineTransformMakeScale(_scaleFactor, _scaleFactor); } - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return desiredHeight / _scaleFactor; } - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component; { return desiredWidth / _scaleFactor; } - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return desiredHeight / _scaleFactor; } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *label = (UILabel *)view; if (!label) { label = [[UILabel alloc] init]; CGFloat desiredFontSize; label.font = [UIFont systemFontOfSize:(desiredFontSize / _scaleFactor)]; } // set text from data source return label; }
与屏蔽不可见行的区别非常微妙。然而,在我看来,右边的缩放选择器看起来更自然......事实上,这有很大的不同。
(使用的比例因子为 0.5)
于 2014-12-20T02:02:59.017 回答
0
尝试以下代码 -
#pragma mark - picker datasource methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 3;
}
#pragma mark - picker delegate
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [YourArray objectAtIndex:row];//your array that contains data
}
于 2013-06-27T12:47:22.823 回答
0
请尝试以下代码:
//Method to define how many columns/dials to show
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
// Method to define the numberOfRows in a component using the array.
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent :(NSInteger)component
{
if (component==0)
{
return [arrFirst count];
}
else if (component==1)
{
return [arrSecond count];
}
else
{
return [arrThird count];
}
}
// Method to show the title of row for a component.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component)
{
case 0:
return [arrFirst objectAtIndex:row];
break;
case 1:
return [arrSecond objectAtIndex:row];
break;
case 2:
return [arrThird objectAtIndex:row];
break;
}
return nil;
}
谢谢,如果您有任何问题,请告诉我。
于 2013-06-27T12:52:10.167 回答